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

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