3 Licensed to the Apache Software Foundation (ASF) under one or more
\r
4 contributor license agreements. See the NOTICE file distributed with
\r
5 this work for additional information regarding copyright ownership.
\r
6 The ASF licenses this file to You under the Apache License, Version 2.0
\r
7 (the "License"); you may not use this file except in compliance with
\r
8 the License. You may obtain a copy of the License at
\r
10 http://www.apache.org/licenses/LICENSE-2.0
\r
12 Unless required by applicable law or agreed to in writing, software
\r
13 distributed under the License is distributed on an "AS IS" BASIS,
\r
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
15 See the License for the specific language governing permissions and
\r
16 limitations under the License.
\r
19 <TITLE>org.apache.lucene.search.function</TITLE>
\r
23 Programmatic control over documents scores.
\r
26 The <code>function</code> package provides tight control over documents scores.
\r
29 <font color="#FF0000">
\r
30 WARNING: The status of the <b>search.function</b> package is experimental. The APIs
\r
31 introduced here might change in the future and will not be supported anymore
\r
36 Two types of queries are available in this package:
\r
41 <b>Custom Score queries</b> - allowing to set the score
\r
42 of a matching document as a mathematical expression over scores
\r
43 of that document by contained (sub) queries.
\r
46 <b>Field score queries</b> - allowing to base the score of a
\r
47 document on <b>numeric values</b> of <b>indexed fields</b>.
\r
53 <b>Some possible uses of these queries:</b>
\r
58 Normalizing the document scores by values indexed in a special field -
\r
59 for instance, experimenting with a different doc length normalization.
\r
62 Introducing some static scoring element, to the score of a document, -
\r
63 for instance using some topological attribute of the links to/from a document.
\r
66 Computing the score of a matching document as an arbitrary odd function of
\r
67 its score by a certain query.
\r
72 <b>Performance and Quality Considerations:</b>
\r
77 When scoring by values of indexed fields,
\r
78 these values are loaded into memory.
\r
79 Unlike the regular scoring, where the required information is read from
\r
80 disk as necessary, here field values are loaded once and cached by Lucene in memory
\r
81 for further use, anticipating reuse by further queries. While all this is carefully
\r
82 cached with performance in mind, it is recommended to
\r
83 use these features only when the default Lucene scoring does
\r
84 not match your "special" application needs.
\r
87 Use only with carefully selected fields, because in most cases,
\r
88 search quality with regular Lucene scoring
\r
89 would outperform that of scoring by field values.
\r
92 Values of fields used for scoring should match.
\r
93 Do not apply on a field containing arbitrary (long) text.
\r
94 Do not mix values in the same field if that field is used for scoring.
\r
97 Smaller (shorter) field tokens means less RAM (something always desired).
\r
98 When using <a href = FieldScoreQuery.html>FieldScoreQuery</a>,
\r
99 select the shortest <a href = FieldScoreQuery.html#Type>FieldScoreQuery.Type</a>
\r
100 that is sufficient for the used field values.
\r
103 Reusing IndexReaders/IndexSearchers is essential, because the caching of field tokens
\r
104 is based on an IndexReader. Whenever a new IndexReader is used, values currently in the cache
\r
105 cannot be used and new values must be loaded from disk. So replace/refresh readers/searchers in
\r
106 a controlled manner.
\r
111 <b>History and Credits:</b>
\r
114 A large part of the code of this package was originated from Yonik's FunctionQuery code that was
\r
115 imported from <a href = "http://lucene.apache.org//solr">Solr</a>
\r
116 (see <a href = "http://issues.apache.org//jira/browse/LUCENE-446">LUCENE-446</a>).
\r
119 The idea behind CustomScoreQurey is borrowed from
\r
120 the "Easily create queries that transform sub-query scores arbitrarily" contribution by Mike Klaas
\r
121 (see <a href = "http://issues.apache.org//jira/browse/LUCENE-850">LUCENE-850</a>)
\r
122 though the implementation and API here are different.
\r
127 <b>Code sample:</b>
\r
129 Note: code snippets here should work, but they were never really compiled... so,
\r
130 tests sources under TestCustomScoreQuery, TestFieldScoreQuery and TestOrdValues
\r
131 may also be useful.
\r
134 Using field (byte) values to as scores:
\r
138 f = new Field("score", "7", Field.Store.NO, Field.Index.UN_TOKENIZED);
\r
139 f.setOmitNorms(true);
\r
145 Query q = new FieldScoreQuery("score", FieldScoreQuery.Type.BYTE);
\r
147 Document d1 above would get a score of 7.
\r
151 Manipulating scores
\r
153 Dividing the original score of each document by a square root of its docid
\r
154 (just to demonstrate what it takes to manipulate scores this way)
\r
156 Query q = queryParser.parse("my query text");
\r
157 CustomScoreQuery customQ = new CustomScoreQuery(q) {
\r
158 public float customScore(int doc, float subQueryScore, float valSrcScore) {
\r
159 return subQueryScore / Math.sqrt(docid);
\r
164 For more informative debug info on the custom query, also override the name() method:
\r
166 CustomScoreQuery customQ = new CustomScoreQuery(q) {
\r
167 public float customScore(int doc, float subQueryScore, float valSrcScore) {
\r
168 return subQueryScore / Math.sqrt(docid);
\r
170 public String name() {
\r
171 return "1/sqrt(docid)";
\r
176 Taking the square root of the original score and multiplying it by a "short field driven score", ie, the
\r
177 short value that was indexed for the scored doc in a certain field:
\r
179 Query q = queryParser.parse("my query text");
\r
180 FieldScoreQuery qf = new FieldScoreQuery("shortScore", FieldScoreQuery.Type.SHORT);
\r
181 CustomScoreQuery customQ = new CustomScoreQuery(q,qf) {
\r
182 public float customScore(int doc, float subQueryScore, float valSrcScore) {
\r
183 return Math.sqrt(subQueryScore) * valSrcScore;
\r
185 public String name() {
\r
186 return "shortVal*sqrt(score)";
\r