pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / java / org / apache / lucene / search / function / OrdFieldSource.java
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 package org.apache.lucene.search.function;
19
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.search.FieldCache;
22
23 import java.io.IOException;
24
25 /**
26  * Expert: obtains the ordinal of the field value from the default Lucene 
27  * {@link org.apache.lucene.search.FieldCache Fieldcache} 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  * @lucene.experimental
43  *
44  * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
45  * searching, if {@link #getValues} is invoked with a
46  * composite (multi-segment) reader, this can easily cause
47  * double RAM usage for the values in the FieldCache.  It's
48  * best to switch your application to pass only atomic
49  * (single segment) readers to this API.</p>
50  */
51
52 public class OrdFieldSource extends ValueSource {
53   protected String field;
54
55   /** 
56    * Constructor for a certain field.
57    * @param field field whose values order is used.  
58    */
59   public OrdFieldSource(String field) {
60     this.field = field;
61   }
62
63   /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
64   @Override
65   public String description() {
66     return "ord(" + field + ')';
67   }
68
69   /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
70   @Override
71   public DocValues getValues(IndexReader reader) throws IOException {
72     final int[] arr = FieldCache.DEFAULT.getStringIndex(reader, field).order;
73     return new DocValues() {
74       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
75       @Override
76       public float floatVal(int doc) {
77         return arr[doc];
78       }
79       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */
80       @Override
81       public String strVal(int doc) {
82         // the string value of the ordinal, not the string itself
83         return Integer.toString(arr[doc]);
84       }
85       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
86       @Override
87       public String toString(int doc) {
88         return description() + '=' + intVal(doc);
89       }
90       /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
91       @Override
92       Object getInnerArray() {
93         return arr;
94       }
95     };
96   }
97
98   /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
99   @Override
100   public boolean equals(Object o) {
101     if (o == this) return true;
102     if (o == null) return false;
103     if (o.getClass() != OrdFieldSource.class) return false;
104     OrdFieldSource other = (OrdFieldSource)o;
105     return this.field.equals(other.field);
106   }
107
108   private static final int hcode = OrdFieldSource.class.hashCode();
109   
110   /*(non-Javadoc) @see java.lang.Object#hashCode() */
111   @Override
112   public int hashCode() {
113     return hcode + field.hashCode();
114   }
115 }