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
DocValues.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 using Explanation = Lucene.Net.Search.Explanation;
21 
22 namespace Lucene.Net.Search.Function
23 {
24 
25  /// <summary> Expert: represents field values as different types.
26  /// Normally created via a
27  /// <see cref="Lucene.Net.Search.Function.ValueSource">ValueSuorce</see>
28  /// for a particular field and reader.
29  ///
30  /// <p/><font color="#FF0000">
31  /// WARNING: The status of the <b>Search.Function</b> package is experimental.
32  /// The APIs introduced here might change in the future and will not be
33  /// supported anymore in such a case.</font>
34  ///
35  ///
36  /// </summary>
37  public abstract class DocValues
38  {
39  /*
40  * DocValues is distinct from ValueSource because
41  * there needs to be an object created at query evaluation time that
42  * is not referenced by the query itself because:
43  * - Query objects should be MT safe
44  * - For caching, Query objects are often used as keys... you don't
45  * want the Query carrying around big objects
46  */
47 
48  /// <summary> Return doc value as a float.
49  /// <p/>Mandatory: every DocValues implementation must implement at least this method.
50  /// </summary>
51  /// <param name="doc">document whose float value is requested.
52  /// </param>
53  public abstract float FloatVal(int doc);
54 
55  /// <summary> Return doc value as an int.
56  /// <p/>Optional: DocValues implementation can (but don't have to) override this method.
57  /// </summary>
58  /// <param name="doc">document whose int value is requested.
59  /// </param>
60  public virtual int IntVal(int doc)
61  {
62  return (int) FloatVal(doc);
63  }
64 
65  /// <summary> Return doc value as a long.
66  /// <p/>Optional: DocValues implementation can (but don't have to) override this method.
67  /// </summary>
68  /// <param name="doc">document whose long value is requested.
69  /// </param>
70  public virtual long LongVal(int doc)
71  {
72  return (long) FloatVal(doc);
73  }
74 
75  /// <summary> Return doc value as a double.
76  /// <p/>Optional: DocValues implementation can (but don't have to) override this method.
77  /// </summary>
78  /// <param name="doc">document whose double value is requested.
79  /// </param>
80  public virtual double DoubleVal(int doc)
81  {
82  return (double) FloatVal(doc);
83  }
84 
85  /// <summary> Return doc value as a string.
86  /// <p/>Optional: DocValues implementation can (but don't have to) override this method.
87  /// </summary>
88  /// <param name="doc">document whose string value is requested.
89  /// </param>
90  public virtual System.String StrVal(int doc)
91  {
92  return FloatVal(doc).ToString();
93  }
94 
95  /// <summary> Return a string representation of a doc value, as reuired for Explanations.</summary>
96  public abstract System.String ToString(int doc);
97 
98  /// <summary> Explain the scoring value for the input doc.</summary>
99  public virtual Explanation Explain(int doc)
100  {
101  return new Explanation(FloatVal(doc), ToString(doc));
102  }
103 
104  /// <summary> Expert: for test purposes only, return the inner array of values, or null if not applicable.
105  /// <p/>
106  /// Allows tests to verify that loaded values are:
107  /// <list type="bullet">
108  /// <item>indeed cached/reused.</item>
109  /// <item>stored in the expected size/type (byte/short/int/float).</item>
110  /// </list>
111  /// Note: implementations of DocValues must override this method for
112  /// these test elements to be tested, Otherwise the test would not fail, just
113  /// print a warning.
114  /// </summary>
115  protected internal virtual object InnerArray
116  {
117  get { throw new System.NotSupportedException("this optional method is for test purposes only"); }
118  }
119 
120  // --- some simple statistics on values
121  private float minVal = System.Single.NaN;
122  private float maxVal = System.Single.NaN;
123  private float avgVal = System.Single.NaN;
124  private bool computed = false;
125  // compute optional values
126  private void Compute()
127  {
128  if (computed)
129  {
130  return ;
131  }
132  float sum = 0;
133  int n = 0;
134  while (true)
135  {
136  float val;
137  try
138  {
139  val = FloatVal(n);
140  }
141  catch (System.IndexOutOfRangeException)
142  {
143  break;
144  }
145  sum += val;
146  minVal = System.Single.IsNaN(minVal)?val:System.Math.Min(minVal, val);
147  maxVal = System.Single.IsNaN(maxVal)?val:System.Math.Max(maxVal, val);
148  ++n;
149  }
150 
151  avgVal = n == 0?System.Single.NaN:sum / n;
152  computed = true;
153  }
154 
155  /// <summary> Returns the minimum of all values or <c>Float.NaN</c> if this
156  /// DocValues instance does not contain any value.
157  /// <p/>
158  /// This operation is optional
159  /// <p/>
160  ///
161  /// </summary>
162  /// <returns> the minimum of all values or <c>Float.NaN</c> if this
163  /// DocValues instance does not contain any value.
164  /// </returns>
165  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
166  public virtual float GetMinValue()
167  {
168  Compute();
169  return minVal;
170  }
171 
172  /// <summary> Returns the maximum of all values or <c>Float.NaN</c> if this
173  /// DocValues instance does not contain any value.
174  /// <p/>
175  /// This operation is optional
176  /// <p/>
177  ///
178  /// </summary>
179  /// <returns> the maximum of all values or <c>Float.NaN</c> if this
180  /// DocValues instance does not contain any value.
181  /// </returns>
182  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
183  public virtual float GetMaxValue()
184  {
185  Compute();
186  return maxVal;
187  }
188 
189  /// <summary> Returns the average of all values or <c>Float.NaN</c> if this
190  /// DocValues instance does not contain any value. *
191  /// <p/>
192  /// This operation is optional
193  /// <p/>
194  ///
195  /// </summary>
196  /// <returns> the average of all values or <c>Float.NaN</c> if this
197  /// DocValues instance does not contain any value
198  /// </returns>
199  [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1024:UsePropertiesWhereAppropriate")]
200  public virtual float GetAverageValue()
201  {
202  Compute();
203  return avgVal;
204  }
205  }
206 }