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

    1   package org.apache.lucene.search.function;
    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.index.TermDocs;
   23   import org.apache.lucene.search;
   24   import org.apache.lucene.util.ToStringUtils;
   25   
   26   import java.io.IOException;
   27   import java.util.Set;
   28   
   29   /**
   30    * Expert: A Query that sets the scores of document to the
   31    * values obtained from a {@link org.apache.lucene.search.function.ValueSource ValueSource}.
   32    * <p>
   33    * This query provides a score for <em>each and every</em> undeleted document in the index.    
   34    * <p>
   35    * The value source can be based on a (cached) value of an indexed field, but it
   36    * can also be based on an external source, e.g. values read from an external database. 
   37    * <p>
   38    * Score is set as: Score(doc,query) = query.getBoost()<sup>2</sup> * valueSource(doc).  
   39    *
   40    * <p><font color="#FF0000">
   41    * WARNING: The status of the <b>search.function</b> package is experimental. 
   42    * The APIs introduced here might change in the future and will not be 
   43    * supported anymore in such a case.</font>
   44    */
   45   public class ValueSourceQuery extends Query {
   46     ValueSource valSrc;
   47   
   48     /**
   49      * Create a value source query
   50      * @param valSrc provides the values defines the function to be used for scoring
   51      */
   52     public ValueSourceQuery(ValueSource valSrc) {
   53       this.valSrc=valSrc;
   54     }
   55   
   56     /*(non-Javadoc) @see org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader) */
   57     @Override
   58     public Query rewrite(IndexReader reader) throws IOException {
   59       return this;
   60     }
   61   
   62     /*(non-Javadoc) @see org.apache.lucene.search.Query#extractTerms(java.util.Set) */
   63     @Override
   64     public void extractTerms(Set<Term> terms) {
   65       // no terms involved here
   66     }
   67   
   68     class ValueSourceWeight extends Weight {
   69       Similarity similarity;
   70       float queryNorm;
   71       float queryWeight;
   72   
   73       public ValueSourceWeight(Searcher searcher) {
   74         this.similarity = getSimilarity(searcher);
   75       }
   76   
   77       /*(non-Javadoc) @see org.apache.lucene.search.Weight#getQuery() */
   78       @Override
   79       public Query getQuery() {
   80         return ValueSourceQuery.this;
   81       }
   82   
   83       /*(non-Javadoc) @see org.apache.lucene.search.Weight#getValue() */
   84       @Override
   85       public float getValue() {
   86         return queryWeight;
   87       }
   88   
   89       /*(non-Javadoc) @see org.apache.lucene.search.Weight#sumOfSquaredWeights() */
   90       @Override
   91       public float sumOfSquaredWeights() throws IOException {
   92         queryWeight = getBoost();
   93         return queryWeight * queryWeight;
   94       }
   95   
   96       /*(non-Javadoc) @see org.apache.lucene.search.Weight#normalize(float) */
   97       @Override
   98       public void normalize(float norm) {
   99         this.queryNorm = norm;
  100         queryWeight *= this.queryNorm;
  101       }
  102   
  103       @Override
  104       public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
  105         return new ValueSourceScorer(similarity, reader, this);
  106       }
  107   
  108       /*(non-Javadoc) @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader, int) */
  109       @Override
  110       public Explanation explain(IndexReader reader, int doc) throws IOException {
  111         DocValues vals = valSrc.getValues(reader);
  112         float sc = queryWeight * vals.floatVal(doc);
  113   
  114         Explanation result = new ComplexExplanation(
  115           true, sc, ValueSourceQuery.this.toString() + ", product of:");
  116   
  117         result.addDetail(vals.explain(doc));
  118         result.addDetail(new Explanation(getBoost(), "boost"));
  119         result.addDetail(new Explanation(queryNorm,"queryNorm"));
  120         return result;
  121       }
  122     }
  123   
  124     /**
  125      * A scorer that (simply) matches all documents, and scores each document with 
  126      * the value of the value source in effect. As an example, if the value source 
  127      * is a (cached) field source, then value of that field in that document will 
  128      * be used. (assuming field is indexed for this doc, with a single token.)   
  129      */
  130     private class ValueSourceScorer extends Scorer {
  131       private final ValueSourceWeight weight;
  132       private final float qWeight;
  133       private final DocValues vals;
  134       private final TermDocs termDocs;
  135       private int doc = -1;
  136   
  137       // constructor
  138       private ValueSourceScorer(Similarity similarity, IndexReader reader, ValueSourceWeight w) throws IOException {
  139         super(similarity);
  140         this.weight = w;
  141         this.qWeight = w.getValue();
  142         // this is when/where the values are first created.
  143         vals = valSrc.getValues(reader);
  144         termDocs = reader.termDocs(null);
  145       }
  146   
  147       @Override
  148       public int nextDoc() throws IOException {
  149         return doc = termDocs.next() ? termDocs.doc() : NO_MORE_DOCS;
  150       }
  151       
  152       @Override
  153       public int docID() {
  154         return doc;
  155       }
  156       
  157       @Override
  158       public int advance(int target) throws IOException {
  159         return doc = termDocs.skipTo(target) ? termDocs.doc() : NO_MORE_DOCS;
  160       }
  161       
  162       /*(non-Javadoc) @see org.apache.lucene.search.Scorer#score() */
  163       @Override
  164       public float score() throws IOException {
  165         return qWeight * vals.floatVal(termDocs.doc());
  166       }
  167     }
  168   
  169     @Override
  170     public Weight createWeight(Searcher searcher) {
  171       return new ValueSourceQuery.ValueSourceWeight(searcher);
  172     }
  173   
  174     @Override
  175     public String toString(String field) {
  176       return valSrc.toString() + ToStringUtils.boost(getBoost());
  177     }
  178   
  179     /** Returns true if <code>o</code> is equal to this. */
  180     @Override
  181     public boolean equals(Object o) {
  182       if (getClass() != o.getClass()) {
  183         return false;
  184       }
  185       ValueSourceQuery other = (ValueSourceQuery)o;
  186       return this.getBoost() == other.getBoost()
  187              && this.valSrc.equals(other.valSrc);
  188     }
  189   
  190     /** Returns a hash code value for this object. */
  191     @Override
  192     public int hashCode() {
  193       return (getClass().hashCode() + valSrc.hashCode()) ^ Float.floatToIntBits(getBoost());
  194     }
  195   
  196   }

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