1 package org.apache.lucene.search.function;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
26 import java.io.IOException;
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}.
33 * This query provides a score for <em>each and every</em> undeleted document in the index.
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.
38 * Score is set as: Score(doc,query) = query.getBoost()<sup>2</sup> * valueSource(doc).
40 * @lucene.experimental
42 public class ValueSourceQuery extends Query {
46 * Create a value source query
47 * @param valSrc provides the values defines the function to be used for scoring
49 public ValueSourceQuery(ValueSource valSrc) {
53 /*(non-Javadoc) @see org.apache.lucene.search.Query#rewrite(org.apache.lucene.index.IndexReader) */
55 public Query rewrite(IndexReader reader) throws IOException {
59 /*(non-Javadoc) @see org.apache.lucene.search.Query#extractTerms(java.util.Set) */
61 public void extractTerms(Set<Term> terms) {
62 // no terms involved here
65 class ValueSourceWeight extends Weight {
66 Similarity similarity;
70 public ValueSourceWeight(Searcher searcher) {
71 this.similarity = getSimilarity(searcher);
74 /*(non-Javadoc) @see org.apache.lucene.search.Weight#getQuery() */
76 public Query getQuery() {
77 return ValueSourceQuery.this;
80 /*(non-Javadoc) @see org.apache.lucene.search.Weight#getValue() */
82 public float getValue() {
86 /*(non-Javadoc) @see org.apache.lucene.search.Weight#sumOfSquaredWeights() */
88 public float sumOfSquaredWeights() throws IOException {
89 queryWeight = getBoost();
90 return queryWeight * queryWeight;
93 /*(non-Javadoc) @see org.apache.lucene.search.Weight#normalize(float) */
95 public void normalize(float norm) {
96 this.queryNorm = norm;
97 queryWeight *= this.queryNorm;
101 public Scorer scorer(IndexReader reader, boolean scoreDocsInOrder, boolean topScorer) throws IOException {
102 return new ValueSourceScorer(similarity, reader, this);
105 /*(non-Javadoc) @see org.apache.lucene.search.Weight#explain(org.apache.lucene.index.IndexReader, int) */
107 public Explanation explain(IndexReader reader, int doc) throws IOException {
108 DocValues vals = valSrc.getValues(reader);
109 float sc = queryWeight * vals.floatVal(doc);
111 Explanation result = new ComplexExplanation(
112 true, sc, ValueSourceQuery.this.toString() + ", product of:");
114 result.addDetail(vals.explain(doc));
115 result.addDetail(new Explanation(getBoost(), "boost"));
116 result.addDetail(new Explanation(queryNorm,"queryNorm"));
122 * A scorer that (simply) matches all documents, and scores each document with
123 * the value of the value source in effect. As an example, if the value source
124 * is a (cached) field source, then value of that field in that document will
125 * be used. (assuming field is indexed for this doc, with a single token.)
127 private class ValueSourceScorer extends Scorer {
128 private final float qWeight;
129 private final DocValues vals;
130 private final TermDocs termDocs;
131 private int doc = -1;
134 private ValueSourceScorer(Similarity similarity, IndexReader reader, ValueSourceWeight w) throws IOException {
136 qWeight = w.getValue();
137 // this is when/where the values are first created.
138 vals = valSrc.getValues(reader);
139 termDocs = reader.termDocs(null);
143 public int nextDoc() throws IOException {
144 return doc = termDocs.next() ? termDocs.doc() : NO_MORE_DOCS;
153 public int advance(int target) throws IOException {
154 return doc = termDocs.skipTo(target) ? termDocs.doc() : NO_MORE_DOCS;
157 /*(non-Javadoc) @see org.apache.lucene.search.Scorer#score() */
159 public float score() throws IOException {
160 return qWeight * vals.floatVal(termDocs.doc());
165 public Weight createWeight(Searcher searcher) {
166 return new ValueSourceQuery.ValueSourceWeight(searcher);
170 public String toString(String field) {
171 return valSrc.toString() + ToStringUtils.boost(getBoost());
174 /** Returns true if <code>o</code> is equal to this. */
176 public boolean equals(Object o) {
179 if (!super.equals(o))
181 if (getClass() != o.getClass()) {
184 ValueSourceQuery other = (ValueSourceQuery)o;
185 return this.getBoost() == other.getBoost()
186 && this.valSrc.equals(other.valSrc);
189 /** Returns a hash code value for this object. */
191 public int hashCode() {
192 return (getClass().hashCode() + valSrc.hashCode()) ^ Float.floatToIntBits(getBoost());