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.solr.search.function;
20
import org.apache.lucene.index.IndexReader;
21
import org.apache.solr.search.function.DocValues;
22
import org.apache.solr.search.function.ValueSource;
23
import org.apache.lucene.search.FieldCache;
25
import java.io.IOException;
29
* Obtains the ordinal of the field value from the default Lucene {@link org.apache.lucene.search.FieldCache} using getStringIndex()
30
* and reverses the order.
32
* The native lucene index order is used to assign an ordinal value for each field value.
33
* <br>Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
35
* Example of reverse ordinal (rord):<br>
36
* If there were only three field values: "apple","banana","pear"
37
* <br>then rord("apple")=3, rord("banana")=2, ord("pear")=1
39
* WARNING: ord() depends on the position in an index and can thus change when other documents are inserted or deleted,
40
* or if a MultiSearcher is used.
42
* WARNING: as of Solr 1.4, ord() and rord() can cause excess memory use since they must use a FieldCache entry
43
* at the top level reader, while sorting and function queries now use entries at the segment level. Hence sorting
44
* or using a different function query, in addition to ord()/rord() will double memory use.
46
* @version $Id: ReverseOrdFieldSource.java 1065312 2011-01-30 16:08:25Z rmuir $
49
public class ReverseOrdFieldSource extends ValueSource {
52
public ReverseOrdFieldSource(String field) {
57
public String description() {
58
return "rord("+field+')';
62
public DocValues getValues(Map context, IndexReader reader) throws IOException {
63
final FieldCache.StringIndex sindex = FieldCache.DEFAULT.getStringIndex(reader, field);
65
final int arr[] = sindex.order;
66
final int end = sindex.lookup.length;
68
return new DocValues() {
70
public float floatVal(int doc) {
71
return (float)(end - arr[doc]);
75
public int intVal(int doc) {
76
return (end - arr[doc]);
80
public long longVal(int doc) {
81
return (long)(end - arr[doc]);
85
public double doubleVal(int doc) {
86
return (double)(end - arr[doc]);
90
public String strVal(int doc) {
91
// the string value of the ordinal, not the string itself
92
return Integer.toString((end - arr[doc]));
96
public String toString(int doc) {
97
return description() + '=' + strVal(doc);
103
public boolean equals(Object o) {
104
if (o.getClass() != ReverseOrdFieldSource.class) return false;
105
ReverseOrdFieldSource other = (ReverseOrdFieldSource)o;
106
return this.field.equals(other.field);
109
private static final int hcode = ReverseOrdFieldSource.class.hashCode();
111
public int hashCode() {
112
return hcode + field.hashCode();