Lucene.Net  3.0.3
Lucene.Net is a port of the Lucene search engine library, written in C# and targeted at .NET runtime users.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Properties Pages
FieldScoreQuery.cs
Go to the documentation of this file.
1 /*
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
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  */
17 
18 using System;
19 
20 namespace Lucene.Net.Search.Function
21 {
22 
23  /// <summary> A query that scores each document as the value of the numeric input field.
24  /// <p/>
25  /// The query matches all documents, and scores each document according to the numeric
26  /// value of that field.
27  /// <p/>
28  /// It is assumed, and expected, that:
29  /// <list type="bullet">
30  /// <item>The field used here is indexed, and has exactly
31  /// one token in every scored document.</item>
32  /// <item>Best if this field is un_tokenized.</item>
33  /// <item>That token is parsable to the selected type.</item>
34  /// </list>
35  /// <p/>
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.
40  /// <p/>
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:
44  /// <list type="bullet">
45  /// <item><see cref="Type.BYTE" /> consumes 1 * maxDocs bytes.</item>
46  /// <item><see cref="Type.SHORT" /> consumes 2 * maxDocs bytes.</item>
47  /// <item><see cref="Type.INT" /> consumes 4 * maxDocs bytes.</item>
48  /// <item><see cref="Type.FLOAT" /> consumes 8 * maxDocs bytes.</item>
49  /// </list>
50  /// <p/>
51  /// <b>Caching:</b>
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!
56  ///
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>
61  /// </summary>
62  [Serializable]
64  {
65 
66  /// <summary> Type of score field, indicating how field values are interpreted/parsed.
67  /// <p/>
68  /// The type selected at search search time should match the data stored in the field.
69  /// Different types have different RAM requirements:
70  /// <list type="bullet">
71  /// <item><see cref="BYTE" /> consumes 1 * maxDocs bytes.</item>
72  /// <item><see cref="SHORT" /> consumes 2 * maxDocs bytes.</item>
73  /// <item><see cref="INT" /> consumes 4 * maxDocs bytes.</item>
74  /// <item><see cref="FLOAT" /> consumes 8 * maxDocs bytes.</item>
75  /// </list>
76  /// </summary>
77  public class Type
78  {
79 
80  /// <summary>field values are interpreted as numeric byte values. </summary>
81  public static readonly Type BYTE = new Type("byte");
82 
83  /// <summary>field values are interpreted as numeric short values. </summary>
84  public static readonly Type SHORT = new Type("short");
85 
86  /// <summary>field values are interpreted as numeric int values. </summary>
87  public static readonly Type INT = new Type("int");
88 
89  /// <summary>field values are interpreted as numeric float values. </summary>
90  public static readonly Type FLOAT = new Type("float");
91 
92  private System.String typeName;
93  internal Type(System.String name)
94  {
95  this.typeName = name;
96  }
97  /*(non-Javadoc) <see cref="java.lang.Object.toString() */
98  public override System.String ToString()
99  {
100  return GetType().FullName + "::" + typeName;
101  }
102  }
103 
104  /// <summary> Create a FieldScoreQuery - a query that scores each document as the value of the numeric input field.
105  /// <p/>
106  /// The <c>type</c> param tells how to parse the field string values into a numeric score value.
107  /// </summary>
108  /// <param name="field">the numeric field to be used.
109  /// </param>
110  /// <param name="type">the type of the field: either
111  /// <see cref="Type.BYTE" />, <see cref="Type.SHORT" />, <see cref="Type.INT" />, or <see cref="Type.FLOAT" />.
112  /// </param>
113  public FieldScoreQuery(System.String field, Type type):base(GetValueSource(field, type))
114  {
115  }
116 
117  // create the appropriate (cached) field value source.
118  private static ValueSource GetValueSource(System.String field, Type type)
119  {
120  if (type == Type.BYTE)
121  {
122  return new ByteFieldSource(field);
123  }
124  if (type == Type.SHORT)
125  {
126  return new ShortFieldSource(field);
127  }
128  if (type == Type.INT)
129  {
130  return new IntFieldSource(field);
131  }
132  if (type == Type.FLOAT)
133  {
134  return new FloatFieldSource(field);
135  }
136  throw new System.ArgumentException(type + " is not a known Field Score Query Type!");
137  }
138  }
139 }