001: /**
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */package org.apache.solr.search.function;
017:
018: import org.apache.lucene.index.IndexReader;
019: import org.apache.solr.search.function.DocValues;
020: import org.apache.solr.search.function.ValueSource;
021: import org.apache.lucene.search.FieldCache;
022:
023: import java.io.IOException;
024:
025: /**
026: * Obtains the ordinal of the field value from the default Lucene {@link org.apache.lucene.search.FieldCache} using getStringIndex()
027: * and reverses the order.
028: * <br>
029: * The native lucene index order is used to assign an ordinal value for each field value.
030: * <br>Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
031: * <br>
032: * Example of reverse ordinal (rord):<br>
033: * If there were only three field values: "apple","banana","pear"
034: * <br>then rord("apple")=3, rord("banana")=2, ord("pear")=1
035: * <p>
036: * WARNING: ord() depends on the position in an index and can thus change when other documents are inserted or deleted,
037: * or if a MultiSearcher is used.
038: * @author yonik
039: * @version $Id: ReverseOrdFieldSource.java 472574 2006-11-08 18:25:52Z yonik $
040: */
041:
042: public class ReverseOrdFieldSource extends ValueSource {
043: public String field;
044:
045: public ReverseOrdFieldSource(String field) {
046: this .field = field;
047: }
048:
049: public String description() {
050: return "rord(" + field + ')';
051: }
052:
053: public DocValues getValues(IndexReader reader) throws IOException {
054: final FieldCache.StringIndex sindex = FieldCache.DEFAULT
055: .getStringIndex(reader, field);
056:
057: final int arr[] = sindex.order;
058: final int end = sindex.lookup.length;
059:
060: return new DocValues() {
061: public float floatVal(int doc) {
062: return (float) (end - arr[doc]);
063: }
064:
065: public int intVal(int doc) {
066: return (int) (end - arr[doc]);
067: }
068:
069: public long longVal(int doc) {
070: return (long) (end - arr[doc]);
071: }
072:
073: public double doubleVal(int doc) {
074: return (double) (end - arr[doc]);
075: }
076:
077: public String strVal(int doc) {
078: // the string value of the ordinal, not the string itself
079: return Integer.toString((end - arr[doc]));
080: }
081:
082: public String toString(int doc) {
083: return description() + '=' + strVal(doc);
084: }
085: };
086: }
087:
088: public boolean equals(Object o) {
089: if (o.getClass() != ReverseOrdFieldSource.class)
090: return false;
091: ReverseOrdFieldSource other = (ReverseOrdFieldSource) o;
092: return this .field.equals(field);
093: }
094:
095: private static final int hcode = ReverseOrdFieldSource.class
096: .hashCode();
097:
098: public int hashCode() {
099: return hcode + field.hashCode();
100: };
101:
102: }
|