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
OrdFieldSource.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 IndexReader = Lucene.Net.Index.IndexReader;
21 using FieldCache = Lucene.Net.Search.FieldCache;
22 
23 namespace Lucene.Net.Search.Function
24 {
25 
26  /// <summary> Expert: obtains the ordinal of the field value from the default Lucene
27  /// <see cref="Lucene.Net.Search.FieldCache">Fieldcache</see> using getStringIndex().
28  /// <p/>
29  /// The native lucene index order is used to assign an ordinal value for each field value.
30  /// <p/>
31  /// Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
32  /// <p/>
33  /// Example:
34  /// <br/>If there were only three field values: "apple","banana","pear"
35  /// <br/>then ord("apple")=1, ord("banana")=2, ord("pear")=3
36  /// <p/>
37  /// WARNING:
38  /// ord() depends on the position in an index and can thus change
39  /// when other documents are inserted or deleted,
40  /// or if a MultiSearcher is used.
41  ///
42  /// <p/><font color="#FF0000">
43  /// WARNING: The status of the <b>Search.Function</b> package is experimental.
44  /// The APIs introduced here might change in the future and will not be
45  /// supported anymore in such a case.</font>
46  ///
47  /// <p/><b>NOTE</b>: with the switch in 2.9 to segment-based
48  /// searching, if <see cref="GetValues" /> is invoked with a
49  /// composite (multi-segment) reader, this can easily cause
50  /// double RAM usage for the values in the FieldCache. It's
51  /// best to switch your application to pass only atomic
52  /// (single segment) readers to this API.<p/>
53  /// </summary>
54 
55  [Serializable]
57  {
58  private class AnonymousClassDocValues:DocValues
59  {
60  public AnonymousClassDocValues(int[] arr, OrdFieldSource enclosingInstance)
61  {
62  InitBlock(arr, enclosingInstance);
63  }
64  private void InitBlock(int[] arr, OrdFieldSource enclosingInstance)
65  {
66  this.arr = arr;
67  this.enclosingInstance = enclosingInstance;
68  }
69  private int[] arr;
70  private OrdFieldSource enclosingInstance;
71  public OrdFieldSource Enclosing_Instance
72  {
73  get
74  {
75  return enclosingInstance;
76  }
77 
78  }
79  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.floatVal(int) */
80  public override float FloatVal(int doc)
81  {
82  return (float) arr[doc];
83  }
84  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.strVal(int) */
85  public override System.String StrVal(int doc)
86  {
87  // the string value of the ordinal, not the string itself
88  return System.Convert.ToString(arr[doc]);
89  }
90  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.toString(int) */
91  public override System.String ToString(int doc)
92  {
93  return Enclosing_Instance.Description() + '=' + IntVal(doc);
94  }
95  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.DocValues.getInnerArray() */
96 
97  protected internal override object InnerArray
98  {
99  get { return arr; }
100  }
101  }
102  protected internal System.String field;
103 
104  /// <summary> Constructor for a certain field.</summary>
105  /// <param name="field">field whose values order is used.
106  /// </param>
107  public OrdFieldSource(System.String field)
108  {
109  this.field = field;
110  }
111 
112  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.ValueSource.description() */
113  public override System.String Description()
114  {
115  return "ord(" + field + ')';
116  }
117 
118  /*(non-Javadoc) <see cref="Lucene.Net.Search.Function.ValueSource.getValues(Lucene.Net.Index.IndexReader) */
119  public override DocValues GetValues(IndexReader reader)
120  {
121  int[] arr = Lucene.Net.Search.FieldCache_Fields.DEFAULT.GetStringIndex(reader, field).order;
122  return new AnonymousClassDocValues(arr, this);
123  }
124 
125  /*(non-Javadoc) <see cref="java.lang.Object.equals(java.lang.Object) */
126  public override bool Equals(System.Object o)
127  {
128  if (o.GetType() != typeof(OrdFieldSource))
129  return false;
130  OrdFieldSource other = (OrdFieldSource) o;
131  return this.field.Equals(other.field);
132  }
133 
134  private static readonly int hcode;
135 
136  /*(non-Javadoc) <see cref="java.lang.Object.hashCode() */
137  public override int GetHashCode()
138  {
139  return hcode + field.GetHashCode();
140  }
141  static OrdFieldSource()
142  {
143  hcode = typeof(OrdFieldSource).GetHashCode();
144  }
145  }
146 }