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.lucene.search.function;
017:
018: import org.apache.lucene.index.IndexReader;
019: import org.apache.lucene.search.FieldCache;
020:
021: import java.io.IOException;
022:
023: /**
024: * Expert: obtains the ordinal of the field value from the default Lucene
025: * {@link org.apache.lucene.search.FieldCache Fieldcache} using getStringIndex().
026: * <p>
027: * The native lucene index order is used to assign an ordinal value for each field value.
028: * <p
029: * Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
030: * <p>
031: * Example:
032: * <br>If there were only three field values: "apple","banana","pear"
033: * <br>then ord("apple")=1, ord("banana")=2, ord("pear")=3
034: * <p>
035: * WARNING:
036: * ord() depends on the position in an index and can thus change
037: * when other documents are inserted or deleted,
038: * or if a MultiSearcher is used.
039: *
040: * <p><font color="#FF0000">
041: * WARNING: The status of the <b>search.function</b> package is experimental.
042: * The APIs introduced here might change in the future and will not be
043: * supported anymore in such a case.</font>
044: *
045: * @author yonik
046: */
047:
048: public class OrdFieldSource extends ValueSource {
049: protected String field;
050:
051: /**
052: * Contructor for a certain field.
053: * @param field field whose values order is used.
054: */
055: public OrdFieldSource(String field) {
056: this .field = field;
057: }
058:
059: /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
060: public String description() {
061: return "ord(" + field + ')';
062: }
063:
064: /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
065: public DocValues getValues(IndexReader reader) throws IOException {
066: final int[] arr = FieldCache.DEFAULT.getStringIndex(reader,
067: field).order;
068: return new DocValues() {
069: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
070: public float floatVal(int doc) {
071: return (float) arr[doc];
072: }
073:
074: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */
075: public String strVal(int doc) {
076: // the string value of the ordinal, not the string itself
077: return Integer.toString(arr[doc]);
078: }
079:
080: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
081: public String toString(int doc) {
082: return description() + '=' + intVal(doc);
083: }
084:
085: /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
086: Object getInnerArray() {
087: return arr;
088: }
089: };
090: }
091:
092: /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
093: public boolean equals(Object o) {
094: if (o.getClass() != OrdFieldSource.class)
095: return false;
096: OrdFieldSource other = (OrdFieldSource) o;
097: return this .field.equals(other.field);
098: }
099:
100: private static final int hcode = OrdFieldSource.class.hashCode();
101:
102: /*(non-Javadoc) @see java.lang.Object#hashCode() */
103: public int hashCode() {
104: return hcode + field.hashCode();
105: }
106: }
|