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()
28 * and reverses the order.
30 * The native lucene index order is used to assign an ordinal value for each field value.
32 * Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
34 * Example of reverse ordinal (rord):
35 * <br>If there were only three field values: "apple","banana","pear"
36 * <br>then rord("apple")=3, rord("banana")=2, ord("pear")=1
39 * rord() depends on the position in an index and can thus change
40 * when other documents are inserted or deleted,
41 * or if a MultiSearcher is used.
43 * @lucene.experimental
45 * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
46 * searching, if {@link #getValues} is invoked with a
47 * composite (multi-segment) reader, this can easily cause
48 * double RAM usage for the values in the FieldCache. It's
49 * best to switch your application to pass only atomic
50 * (single segment) readers to this API.</p>
53 public class ReverseOrdFieldSource extends ValueSource {
57 * Contructor for a certain field.
58 * @param field field whose values reverse order is used.
60 public ReverseOrdFieldSource(String field) {
64 /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
66 public String description() {
67 return "rord("+field+')';
70 /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
72 public DocValues getValues(IndexReader reader) throws IOException {
73 final FieldCache.StringIndex sindex = FieldCache.DEFAULT.getStringIndex(reader, field);
75 final int arr[] = sindex.order;
76 final int end = sindex.lookup.length;
78 return new DocValues() {
79 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
81 public float floatVal(int doc) {
82 return (end - arr[doc]);
84 /* (non-Javadoc) @see org.apache.lucene.search.function.DocValues#intVal(int) */
86 public int intVal(int doc) {
87 return end - arr[doc];
89 /* (non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */
91 public String strVal(int doc) {
92 // the string value of the ordinal, not the string itself
93 return Integer.toString(intVal(doc));
95 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
97 public String toString(int doc) {
98 return description() + '=' + strVal(doc);
100 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
102 Object getInnerArray() {
108 /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
110 public boolean equals(Object o) {
111 if (o == this) return true;
112 if (o == null) return false;
113 if (o.getClass() != ReverseOrdFieldSource.class) return false;
114 ReverseOrdFieldSource other = (ReverseOrdFieldSource)o;
115 return this.field.equals(other.field);
118 private static final int hcode = ReverseOrdFieldSource.class.hashCode();
120 /*(non-Javadoc) @see java.lang.Object#hashCode() */
122 public int hashCode() {
123 return hcode + field.hashCode();