3 Licensed to the Apache Software Foundation (ASF) under one or more
4 contributor license agreements. See the NOTICE file distributed with
5 this work for additional information regarding copyright ownership.
6 The ASF licenses this file to You under the Apache License, Version 2.0
7 (the "License"); you may not use this file except in compliance with
8 the License. You may obtain a copy of the License at
10 http://www.apache.org/licenses/LICENSE-2.0
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
19 <TITLE>org.apache.lucene.search.function</TITLE>
23 Programmatic control over documents scores.
26 The <code>function</code> package provides tight control over documents scores.
32 Two types of queries are available in this package:
37 <b>Custom Score queries</b> - allowing to set the score
38 of a matching document as a mathematical expression over scores
39 of that document by contained (sub) queries.
42 <b>Field score queries</b> - allowing to base the score of a
43 document on <b>numeric values</b> of <b>indexed fields</b>.
49 <b>Some possible uses of these queries:</b>
54 Normalizing the document scores by values indexed in a special field -
55 for instance, experimenting with a different doc length normalization.
58 Introducing some static scoring element, to the score of a document, -
59 for instance using some topological attribute of the links to/from a document.
62 Computing the score of a matching document as an arbitrary odd function of
63 its score by a certain query.
68 <b>Performance and Quality Considerations:</b>
73 When scoring by values of indexed fields,
74 these values are loaded into memory.
75 Unlike the regular scoring, where the required information is read from
76 disk as necessary, here field values are loaded once and cached by Lucene in memory
77 for further use, anticipating reuse by further queries. While all this is carefully
78 cached with performance in mind, it is recommended to
79 use these features only when the default Lucene scoring does
80 not match your "special" application needs.
83 Use only with carefully selected fields, because in most cases,
84 search quality with regular Lucene scoring
85 would outperform that of scoring by field values.
88 Values of fields used for scoring should match.
89 Do not apply on a field containing arbitrary (long) text.
90 Do not mix values in the same field if that field is used for scoring.
93 Smaller (shorter) field tokens means less RAM (something always desired).
94 When using <a href=FieldScoreQuery.html>FieldScoreQuery</a>,
95 select the shortest <a href=FieldScoreQuery.html#Type>FieldScoreQuery.Type</a>
96 that is sufficient for the used field values.
99 Reusing IndexReaders/IndexSearchers is essential, because the caching of field tokens
100 is based on an IndexReader. Whenever a new IndexReader is used, values currently in the cache
101 cannot be used and new values must be loaded from disk. So replace/refresh readers/searchers in
107 <b>History and Credits:</b>
110 A large part of the code of this package was originated from Yonik's FunctionQuery code that was
111 imported from <a href="http://lucene.apache.org/solr">Solr</a>
112 (see <a href="http://issues.apache.org/jira/browse/LUCENE-446">LUCENE-446</a>).
115 The idea behind CustomScoreQurey is borrowed from
116 the "Easily create queries that transform sub-query scores arbitrarily" contribution by Mike Klaas
117 (see <a href="http://issues.apache.org/jira/browse/LUCENE-850">LUCENE-850</a>)
118 though the implementation and API here are different.
125 Note: code snippets here should work, but they were never really compiled... so,
126 tests sources under TestCustomScoreQuery, TestFieldScoreQuery and TestOrdValues
130 Using field (byte) values to as scores:
133 <pre class="prettyprint">
134 f = new Field("score", "7", Field.Store.NO, Field.Index.UN_TOKENIZED);
135 f.setOmitNorms(true);
140 <pre class="prettyprint">
141 Query q = new FieldScoreQuery("score", FieldScoreQuery.Type.BYTE);
143 Document d1 above would get a score of 7.
149 Dividing the original score of each document by a square root of its docid
150 (just to demonstrate what it takes to manipulate scores this way)
151 <pre class="prettyprint">
152 Query q = queryParser.parse("my query text");
153 CustomScoreQuery customQ = new CustomScoreQuery(q) {
154 public float customScore(int doc, float subQueryScore, float valSrcScore) {
155 return subQueryScore / Math.sqrt(docid);
160 For more informative debug info on the custom query, also override the name() method:
161 <pre class="prettyprint">
162 CustomScoreQuery customQ = new CustomScoreQuery(q) {
163 public float customScore(int doc, float subQueryScore, float valSrcScore) {
164 return subQueryScore / Math.sqrt(docid);
166 public String name() {
167 return "1/sqrt(docid)";
172 Taking the square root of the original score and multiplying it by a "short field driven score", ie, the
173 short value that was indexed for the scored doc in a certain field:
174 <pre class="prettyprint">
175 Query q = queryParser.parse("my query text");
176 FieldScoreQuery qf = new FieldScoreQuery("shortScore", FieldScoreQuery.Type.SHORT);
177 CustomScoreQuery customQ = new CustomScoreQuery(q,qf) {
178 public float customScore(int doc, float subQueryScore, float valSrcScore) {
179 return Math.sqrt(subQueryScore) * valSrcScore;
181 public String name() {
182 return "shortVal*sqrt(score)";