Home » lucene-3.0.1-src » org.apache » lucene » search » function » [javadoc | source]

    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.lucene.search.function;
   19   
   20   import org.apache.lucene.index.IndexReader;
   21   import org.apache.lucene.search.FieldCache;
   22   
   23   import java.io.IOException;
   24   
   25   /**
   26    * Expert: obtains the ordinal of the field value from the default Lucene 
   27    * {@link org.apache.lucene.search.FieldCache Fieldcache} using getStringIndex().
   28    * <p>
   29    * The native lucene index order is used to assign an ordinal value for each field value.
   30    * <p
   31    * Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1.
   32    * <p>
   33    * Example:
   34    * <br>If there were only three field values: "apple","banana","pear"
   35    * <br>then ord("apple")=1, ord("banana")=2, ord("pear")=3
   36    * <p>
   37    * WARNING: 
   38    * ord() depends on the position in an index and can thus change 
   39    * when other documents are inserted or deleted,
   40    * or if a MultiSearcher is used. 
   41    *
   42    * <p><font color="#FF0000">
   43    * WARNING: The status of the <b>search.function</b> package is experimental. 
   44    * The APIs introduced here might change in the future and will not be 
   45    * supported anymore in such a case.</font>
   46    *
   47    * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
   48    * searching, if {@link #getValues} is invoked with a
   49    * composite (multi-segment) reader, this can easily cause
   50    * double RAM usage for the values in the FieldCache.  It's
   51    * best to switch your application to pass only atomic
   52    * (single segment) readers to this API.</p>
   53    */
   54   
   55   public class OrdFieldSource extends ValueSource {
   56     protected String field;
   57   
   58     /** 
   59      * Constructor for a certain field.
   60      * @param field field whose values order is used.  
   61      */
   62     public OrdFieldSource(String field) {
   63       this.field = field;
   64     }
   65   
   66     /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
   67     @Override
   68     public String description() {
   69       return "ord(" + field + ')';
   70     }
   71   
   72     /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
   73     @Override
   74     public DocValues getValues(IndexReader reader) throws IOException {
   75       final int[] arr = FieldCache.DEFAULT.getStringIndex(reader, field).order;
   76       return new DocValues() {
   77         /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
   78         @Override
   79         public float floatVal(int doc) {
   80           return (float)arr[doc];
   81         }
   82         /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#strVal(int) */
   83         @Override
   84         public String strVal(int doc) {
   85           // the string value of the ordinal, not the string itself
   86           return Integer.toString(arr[doc]);
   87         }
   88         /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
   89         @Override
   90         public String toString(int doc) {
   91           return description() + '=' + intVal(doc);
   92         }
   93         /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
   94         @Override
   95         Object getInnerArray() {
   96           return arr;
   97         }
   98       };
   99     }
  100   
  101     /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
  102     @Override
  103     public boolean equals(Object o) {
  104       if (o.getClass() !=  OrdFieldSource.class) return false;
  105       OrdFieldSource other = (OrdFieldSource)o;
  106       return this.field.equals(other.field);
  107     }
  108   
  109     private static final int hcode = OrdFieldSource.class.hashCode();
  110     
  111     /*(non-Javadoc) @see java.lang.Object#hashCode() */
  112     @Override
  113     public int hashCode() {
  114       return hcode + field.hashCode();
  115     }
  116   }

Home » lucene-3.0.1-src » org.apache » lucene » search » function » [javadoc | source]