add --shared
[pylucene.git] / lucene-java-3.4.0 / lucene / src / java / org / apache / lucene / search / function / package.html
1 <HTML>
2 <!--
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
9
10      http://www.apache.org/licenses/LICENSE-2.0
11
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.
17 -->
18 <HEAD>
19   <TITLE>org.apache.lucene.search.function</TITLE>
20 </HEAD>
21 <BODY>
22 <DIV>
23   Programmatic control over documents scores.
24 </DIV>
25 <DIV>
26   The <code>function</code> package provides tight control over documents scores.
27 </DIV>
28 <DIV>
29 @lucene.experimental
30 </DIV>
31 <DIV>
32   Two types of queries are available in this package:
33 </DIV>
34 <DIV>
35   <ol>
36      <li>
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.
40      </li>
41      <li>
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>.
44      </li>
45   </ol>
46 </DIV>
47 <DIV>&nbsp;</DIV>
48 <DIV>
49   <b>Some possible uses of these queries:</b>
50 </DIV>
51 <DIV>
52   <ol>
53      <li>
54         Normalizing the document scores by values indexed in a special field -
55         for instance, experimenting with a different doc length normalization.
56      </li>
57      <li>
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.
60      </li>
61      <li>
62         Computing the score of a matching document as an arbitrary odd function of
63         its score by a certain query.
64      </li>
65   </ol>
66 </DIV>
67 <DIV>
68   <b>Performance and Quality Considerations:</b>
69 </DIV>
70 <DIV>
71   <ol>
72      <li>
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.
81      </li>
82      <li>
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.
86      </li>
87      <li>
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.
91      </li>
92      <li>
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.
97      </li>
98      <li>
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
102         a controlled manner.
103      </li>
104   </ol>
105 </DIV>
106 <DIV>
107   <b>History and Credits:</b>
108   <ul>
109     <li>
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>).
113     </li>
114     <li>
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.
119     </li>
120   </ul>
121 </DIV>
122 <DIV>
123  <b>Code sample:</b>
124  <P>
125  Note: code snippets here should work, but they were never really compiled... so,
126  tests sources under TestCustomScoreQuery, TestFieldScoreQuery and TestOrdValues
127  may also be useful.
128  <ol>
129   <li>
130     Using field (byte) values to as scores:
131     <p>
132     Indexing:
133     <pre class="prettyprint">
134       f = new Field("score", "7", Field.Store.NO, Field.Index.UN_TOKENIZED);
135       f.setOmitNorms(true);
136       d1.add(f);
137     </pre>
138     <p>
139     Search:
140     <pre class="prettyprint">
141       Query q = new FieldScoreQuery("score", FieldScoreQuery.Type.BYTE);
142     </pre>
143     Document d1 above would get a score of 7.
144   </li>
145   <p>
146   <li>
147     Manipulating scores
148     <p>
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);
156         }
157       };
158     </pre>
159         <p>
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);
165         }
166         public String name() {
167           return "1/sqrt(docid)";
168         }
169       };
170     </pre>
171         <p>
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;
180         }
181         public String name() {
182           return "shortVal*sqrt(score)";
183         }
184       };
185     </pre>
186
187   </li>
188  </ol>
189 </DIV>
190 </BODY>
191 </HTML>