2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 namespace Mono.Lucene.Net.Search.Function
23 /// <summary> A query that scores each document as the value of the numeric input field.
25 /// The query matches all documents, and scores each document according to the numeric
26 /// value of that field.
28 /// It is assumed, and expected, that:
30 /// <li>The field used here is indexed, and has exactly
31 /// one token in every scored document.</li>
32 /// <li>Best if this field is un_tokenized.</li>
33 /// <li>That token is parsable to the selected type.</li>
36 /// Combining this query in a FunctionQuery allows much freedom in affecting document scores.
37 /// Note, that with this freedom comes responsibility: it is more than likely that the
38 /// default Lucene scoring is superior in quality to scoring modified as explained here.
39 /// However, in some cases, and certainly for research experiments, this capability may turn useful.
41 /// When contructing this query, select the appropriate type. That type should match the data stored in the
42 /// field. So in fact the "right" type should be selected before indexing. Type selection
43 /// has effect on the RAM usage:
45 /// <li>{@link Type#BYTE} consumes 1 * maxDocs bytes.</li>
46 /// <li>{@link Type#SHORT} consumes 2 * maxDocs bytes.</li>
47 /// <li>{@link Type#INT} consumes 4 * maxDocs bytes.</li>
48 /// <li>{@link Type#FLOAT} consumes 8 * maxDocs bytes.</li>
52 /// Values for the numeric field are loaded once and cached in memory for further use with the same IndexReader.
53 /// To take advantage of this, it is extremely important to reuse index-readers or index-searchers,
54 /// otherwise, for instance if for each query a new index reader is opened, large penalties would be
55 /// paid for loading the field values into memory over and over again!
57 /// <p/><font color="#FF0000">
58 /// WARNING: The status of the <b>Search.Function</b> package is experimental.
59 /// The APIs introduced here might change in the future and will not be
60 /// supported anymore in such a case.</font>
63 public class FieldScoreQuery:ValueSourceQuery
66 /// <summary> Type of score field, indicating how field values are interpreted/parsed.
68 /// The type selected at search search time should match the data stored in the field.
69 /// Different types have different RAM requirements:
71 /// <li>{@link #BYTE} consumes 1 * maxDocs bytes.</li>
72 /// <li>{@link #SHORT} consumes 2 * maxDocs bytes.</li>
73 /// <li>{@link #INT} consumes 4 * maxDocs bytes.</li>
74 /// <li>{@link #FLOAT} consumes 8 * maxDocs bytes.</li>
80 /// <summary>field values are interpreted as numeric byte values. </summary>
81 public static readonly Type BYTE = new Type("byte");
83 /// <summary>field values are interpreted as numeric short values. </summary>
84 public static readonly Type SHORT = new Type("short");
86 /// <summary>field values are interpreted as numeric int values. </summary>
87 public static readonly Type INT = new Type("int");
89 /// <summary>field values are interpreted as numeric float values. </summary>
90 public static readonly Type FLOAT = new Type("float");
92 private System.String typeName;
93 internal Type(System.String name)
97 /*(non-Javadoc) @see java.lang.Object#toString() */
98 public override System.String ToString()
100 return GetType().FullName + "::" + typeName;
104 /// <summary> Create a FieldScoreQuery - a query that scores each document as the value of the numeric input field.
106 /// The <code>type</code> param tells how to parse the field string values into a numeric score value.
108 /// <param name="field">the numeric field to be used.
110 /// <param name="type">the type of the field: either
111 /// {@link Type#BYTE}, {@link Type#SHORT}, {@link Type#INT}, or {@link Type#FLOAT}.
113 public FieldScoreQuery(System.String field, Type type):base(GetValueSource(field, type))
117 // create the appropriate (cached) field value source.
118 private static ValueSource GetValueSource(System.String field, Type type)
120 if (type == Type.BYTE)
122 return new ByteFieldSource(field);
124 if (type == Type.SHORT)
126 return new ShortFieldSource(field);
128 if (type == Type.INT)
130 return new IntFieldSource(field);
132 if (type == Type.FLOAT)
134 return new FloatFieldSource(field);
136 throw new System.ArgumentException(type + " is not a known Field Score Query Type!");