X-Git-Url: https://git.mdrn.pl/pylucene.git/blobdiff_plain/a2e61f0c04805cfcb8706176758d1283c7e3a55c..aaeed5504b982cf3545252ab528713250aa33eed:/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/search/function/OrdFieldSource.java diff --git a/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/search/function/OrdFieldSource.java b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/search/function/OrdFieldSource.java new file mode 100644 index 0000000..1a85098 --- /dev/null +++ b/lucene-java-3.5.0/lucene/src/java/org/apache/lucene/search/function/OrdFieldSource.java @@ -0,0 +1,115 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.search.function; + +import org.apache.lucene.index.IndexReader; +import org.apache.lucene.search.FieldCache; + +import java.io.IOException; + +/** + * Expert: obtains the ordinal of the field value from the default Lucene + * {@link org.apache.lucene.search.FieldCache Fieldcache} using getStringIndex(). + *

+ * The native lucene index order is used to assign an ordinal value for each field value. + *

+ * Example: + *
If there were only three field values: "apple","banana","pear" + *
then ord("apple")=1, ord("banana")=2, ord("pear")=3 + *

+ * WARNING: + * ord() depends on the position in an index and can thus change + * when other documents are inserted or deleted, + * or if a MultiSearcher is used. + * + * @lucene.experimental + * + *

NOTE: with the switch in 2.9 to segment-based + * searching, if {@link #getValues} is invoked with a + * composite (multi-segment) reader, this can easily cause + * double RAM usage for the values in the FieldCache. It's + * best to switch your application to pass only atomic + * (single segment) readers to this API.

+ */ + +public class OrdFieldSource extends ValueSource { + protected String field; + + /** + * Constructor for a certain field. + * @param field field whose values order is used. + */ + public OrdFieldSource(String field) { + this.field = field; + } + + /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */ + @Override + public String description() { + return "ord(" + field + ')'; + } + + /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */ + @Override + public DocValues getValues(IndexReader reader) throws IOException { + final int[] arr = FieldCache.DEFAULT.getStringIndex(reader, field).order; + return new DocValues() { + /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */ + @Override + public float floatVal(int doc) { + return arr[doc]; + } + /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */ + @Override + public String strVal(int doc) { + // the string value of the ordinal, not the string itself + return Integer.toString(arr[doc]); + } + /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */ + @Override + public String toString(int doc) { + return description() + '=' + intVal(doc); + } + /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */ + @Override + Object getInnerArray() { + return arr; + } + }; + } + + /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */ + @Override + public boolean equals(Object o) { + if (o == this) return true; + if (o == null) return false; + if (o.getClass() != OrdFieldSource.class) return false; + OrdFieldSource other = (OrdFieldSource)o; + return this.field.equals(other.field); + } + + private static final int hcode = OrdFieldSource.class.hashCode(); + + /*(non-Javadoc) @see java.lang.Object#hashCode() */ + @Override + public int hashCode() { + return hcode + field.hashCode(); + } +}