01: /**
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: */package org.apache.solr.search.function;
17:
18: import org.apache.lucene.index.IndexReader;
19: import org.apache.solr.search.function.DocValues;
20: import org.apache.solr.search.function.ValueSource;
21: import org.apache.lucene.search.FieldCache;
22:
23: import java.io.IOException;
24:
25: /**
26: * Obtains the ordinal of the field value from the default Lucene {@link org.apache.lucene.search.FieldCache} using getStringIndex().
27: * <br>
28: * The native lucene index order is used to assign an ordinal value for each field value.
29: * <br>Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
30: * <br>
31: * Example:<br>
32: * If there were only three field values: "apple","banana","pear"
33: * <br>then ord("apple")=1, ord("banana")=2, ord("pear")=3
34: * <p>
35: * WARNING: ord() depends on the position in an index and can thus change when other documents are inserted or deleted,
36: * or if a MultiSearcher is used.
37: * @author yonik
38: * @version $Id: OrdFieldSource.java 472574 2006-11-08 18:25:52Z yonik $
39: */
40:
41: public class OrdFieldSource extends ValueSource {
42: protected String field;
43:
44: public OrdFieldSource(String field) {
45: this .field = field;
46: }
47:
48: public String description() {
49: return "ord(" + field + ')';
50: }
51:
52: public DocValues getValues(IndexReader reader) throws IOException {
53: final int[] arr = FieldCache.DEFAULT.getStringIndex(reader,
54: field).order;
55: return new DocValues() {
56: public float floatVal(int doc) {
57: return (float) arr[doc];
58: }
59:
60: public int intVal(int doc) {
61: return (int) arr[doc];
62: }
63:
64: public long longVal(int doc) {
65: return (long) arr[doc];
66: }
67:
68: public double doubleVal(int doc) {
69: return (double) arr[doc];
70: }
71:
72: public String strVal(int doc) {
73: // the string value of the ordinal, not the string itself
74: return Integer.toString(arr[doc]);
75: }
76:
77: public String toString(int doc) {
78: return description() + '=' + intVal(doc);
79: }
80: };
81: }
82:
83: public boolean equals(Object o) {
84: if (o.getClass() != OrdFieldSource.class)
85: return false;
86: OrdFieldSource other = (OrdFieldSource) o;
87: return this .field.equals(field);
88: }
89:
90: private static final int hcode = OrdFieldSource.class.hashCode();
91:
92: public int hashCode() {
93: return hcode + field.hashCode();
94: };
95:
96: }
|