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.
18
package org.apache.lucene.search.function;
20
import org.apache.lucene.index.IndexReader;
21
import org.apache.lucene.search.FieldCache;
23
import java.io.IOException;
26
* Expert: obtains the ordinal of the field value from the default Lucene
27
* {@link org.apache.lucene.search.FieldCache Fieldcache} using getStringIndex().
29
* The native lucene index order is used to assign an ordinal value for each field value.
31
* Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
34
* <br>If there were only three field values: "apple","banana","pear"
35
* <br>then ord("apple")=1, ord("banana")=2, ord("pear")=3
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.
42
* @lucene.experimental
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>
52
public class OrdFieldSource extends ValueSource {
53
protected String field;
56
* Constructor for a certain field.
57
* @param field field whose values order is used.
59
public OrdFieldSource(String field) {
63
/*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
65
public String description() {
66
return "ord(" + field + ')';
69
/*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
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) */
76
public float floatVal(int doc) {
79
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */
81
public String strVal(int doc) {
82
// the string value of the ordinal, not the string itself
83
return Integer.toString(arr[doc]);
85
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
87
public String toString(int doc) {
88
return description() + '=' + intVal(doc);
90
/*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
92
Object getInnerArray() {
98
/*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
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);
108
private static final int hcode = OrdFieldSource.class.hashCode();
110
/*(non-Javadoc) @see java.lang.Object#hashCode() */
112
public int hashCode() {
113
return hcode + field.hashCode();