Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]

    1   package org.apache.lucene.search.spans;
    2   
    3   /**
    4    * Licensed to the Apache Software Foundation (ASF) under one or more
    5    * contributor license agreements.  See the NOTICE file distributed with
    6    * this work for additional information regarding copyright ownership.
    7    * The ASF licenses this file to You under the Apache License, Version 2.0
    8    * (the "License"); you may not use this file except in compliance with
    9    * the License.  You may obtain a copy of the License at
   10    *
   11    *     http://www.apache.org/licenses/LICENSE-2.0
   12    *
   13    * Unless required by applicable law or agreed to in writing, software
   14    * distributed under the License is distributed on an "AS IS" BASIS,
   15    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   16    * See the License for the specific language governing permissions and
   17    * limitations under the License.
   18    */
   19   
   20   import org.apache.lucene.index.IndexReader;
   21   import org.apache.lucene.index.Term;
   22   import org.apache.lucene.search;
   23   import org.apache.lucene.search.Explanation.IDFExplanation;
   24   
   25   import java.io.IOException;
   26   import java.util.HashSet;
   27   import java.util.Set;
   28   
   29   /**
   30    * Expert-only.  Public for use by other weight implementations
   31    */
   32   public class SpanWeight extends Weight {
   33     protected Similarity similarity;
   34     protected float value;
   35     protected float idf;
   36     protected float queryNorm;
   37     protected float queryWeight;
   38   
   39     protected Set<Term> terms;
   40     protected SpanQuery query;
   41     private IDFExplanation idfExp;
   42   
   43     public SpanWeight(SpanQuery query, Searcher searcher)
   44       throws IOException {
   45       this.similarity = query.getSimilarity(searcher);
   46       this.query = query;
   47       
   48       terms=new HashSet<Term>();
   49       query.extractTerms(terms);
   50       
   51       idfExp = similarity.idfExplain(terms, searcher);
   52       idf = idfExp.getIdf();
   53     }
   54   
   55     @Override
   56     public Query getQuery() { return query; }
   57   
   58     @Override
   59     public float getValue() { return value; }
   60   
   61     @Override
   62     public float sumOfSquaredWeights() throws IOException {
   63       queryWeight = idf * query.getBoost();         // compute query weight
   64       return queryWeight * queryWeight;             // square it
   65     }
   66   
   67     @Override
   68     public void normalize(float queryNorm) {
   69       this.queryNorm = queryNorm;
   70       queryWeight *= queryNorm;                     // normalize query weight
   71       value = queryWeight * idf;                    // idf for document
   72     }
   73   
   74     @Override
   75     public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
   76       return new SpanScorer(query.getSpans(reader), this, similarity, reader
   77           .norms(query.getField()));
   78     }
   79   
   80     @Override
   81     public Explanation explain(IndexReader reader, int doc)
   82       throws IOException {
   83   
   84       ComplexExplanation result = new ComplexExplanation();
   85       result.setDescription("weight("+getQuery()+" in "+doc+"), product of:");
   86       String field = ((SpanQuery)getQuery()).getField();
   87   
   88       Explanation idfExpl =
   89         new Explanation(idf, "idf(" + field + ": " + idfExp.explain() + ")");
   90   
   91       // explain query weight
   92       Explanation queryExpl = new Explanation();
   93       queryExpl.setDescription("queryWeight(" + getQuery() + "), product of:");
   94   
   95       Explanation boostExpl = new Explanation(getQuery().getBoost(), "boost");
   96       if (getQuery().getBoost() != 1.0f)
   97         queryExpl.addDetail(boostExpl);
   98       queryExpl.addDetail(idfExpl);
   99   
  100       Explanation queryNormExpl = new Explanation(queryNorm,"queryNorm");
  101       queryExpl.addDetail(queryNormExpl);
  102   
  103       queryExpl.setValue(boostExpl.getValue() *
  104                          idfExpl.getValue() *
  105                          queryNormExpl.getValue());
  106   
  107       result.addDetail(queryExpl);
  108   
  109       // explain field weight
  110       ComplexExplanation fieldExpl = new ComplexExplanation();
  111       fieldExpl.setDescription("fieldWeight("+field+":"+query.toString(field)+
  112                                " in "+doc+"), product of:");
  113   
  114       Explanation tfExpl = ((SpanScorer)scorer(reader, true, false)).explain(doc);
  115       fieldExpl.addDetail(tfExpl);
  116       fieldExpl.addDetail(idfExpl);
  117   
  118       Explanation fieldNormExpl = new Explanation();
  119       byte[] fieldNorms = reader.norms(field);
  120       float fieldNorm =
  121         fieldNorms!=null ? Similarity.decodeNorm(fieldNorms[doc]) : 1.0f;
  122       fieldNormExpl.setValue(fieldNorm);
  123       fieldNormExpl.setDescription("fieldNorm(field="+field+", doc="+doc+")");
  124       fieldExpl.addDetail(fieldNormExpl);
  125   
  126       fieldExpl.setMatch(Boolean.valueOf(tfExpl.isMatch()));
  127       fieldExpl.setValue(tfExpl.getValue() *
  128                          idfExpl.getValue() *
  129                          fieldNormExpl.getValue());
  130   
  131       result.addDetail(fieldExpl);
  132       result.setMatch(fieldExpl.getMatch());
  133   
  134       // combine them
  135       result.setValue(queryExpl.getValue() * fieldExpl.getValue());
  136   
  137       if (queryExpl.getValue() == 1.0f)
  138         return fieldExpl;
  139   
  140       return result;
  141     }
  142   }

Home » lucene-3.0.1-src » org.apache » lucene » search » spans » [javadoc | source]