~slub.team/goobi-indexserver/3.x

« back to all changes in this revision

Viewing changes to solr/core/src/java/org/apache/solr/search/function/ReverseOrdFieldSource.java

  • Committer: Sebastian Meyer
  • Date: 2012-08-03 09:12:40 UTC
  • Revision ID: sebastian.meyer@slub-dresden.de-20120803091240-x6861b0vabq1xror
Remove Lucene and Solr source code and add patches instead
Fix Bug #985487: Auto-suggestion for the search interface

Show diffs side-by-side

added added

removed removed

Lines of Context:
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.solr.search.function;
19
 
 
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;
24
 
 
25
 
import java.io.IOException;
26
 
import java.util.Map;
27
 
 
28
 
/**
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.
31
 
 * <br>
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.
34
 
 * <br>
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
38
 
 * <p>
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.
41
 
 * <br>
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.
45
 
 * 
46
 
 * @version $Id: ReverseOrdFieldSource.java 1065312 2011-01-30 16:08:25Z rmuir $
47
 
 */
48
 
 
49
 
public class ReverseOrdFieldSource extends ValueSource {
50
 
  public String field;
51
 
 
52
 
  public ReverseOrdFieldSource(String field) {
53
 
    this.field = field;
54
 
  }
55
 
 
56
 
  @Override
57
 
  public String description() {
58
 
    return "rord("+field+')';
59
 
  }
60
 
 
61
 
  @Override
62
 
  public DocValues getValues(Map context, IndexReader reader) throws IOException {
63
 
    final FieldCache.StringIndex sindex = FieldCache.DEFAULT.getStringIndex(reader, field);
64
 
 
65
 
    final int arr[] = sindex.order;
66
 
    final int end = sindex.lookup.length;
67
 
 
68
 
    return new DocValues() {
69
 
      @Override
70
 
      public float floatVal(int doc) {
71
 
        return (float)(end - arr[doc]);
72
 
      }
73
 
 
74
 
      @Override
75
 
      public int intVal(int doc) {
76
 
        return (end - arr[doc]);
77
 
      }
78
 
 
79
 
      @Override
80
 
      public long longVal(int doc) {
81
 
        return (long)(end - arr[doc]);
82
 
      }
83
 
 
84
 
      @Override
85
 
      public double doubleVal(int doc) {
86
 
        return (double)(end - arr[doc]);
87
 
      }
88
 
 
89
 
      @Override
90
 
      public String strVal(int doc) {
91
 
        // the string value of the ordinal, not the string itself
92
 
        return Integer.toString((end - arr[doc]));
93
 
      }
94
 
 
95
 
      @Override
96
 
      public String toString(int doc) {
97
 
        return description() + '=' + strVal(doc);
98
 
      }
99
 
    };
100
 
  }
101
 
 
102
 
  @Override
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);
107
 
  }
108
 
 
109
 
  private static final int hcode = ReverseOrdFieldSource.class.hashCode();
110
 
  @Override
111
 
  public int hashCode() {
112
 
    return hcode + field.hashCode();
113
 
  };
114
 
 
115
 
}