All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.solr.search.function.OrdFieldSource Maven / Gradle / Ivy

The newest version!
/**
 * 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.solr.search.function;

import org.apache.lucene.index.IndexReader;
import org.apache.solr.search.function.DocValues;
import org.apache.solr.search.function.ValueSource;

import java.io.IOException;
import java.util.Map;

/**
 * Obtains the ordinal of the field value from the default Lucene {@link org.apache.lucene.search.FieldCache} using getStringIndex().
 * 
* The native lucene index order is used to assign an ordinal value for each field value. *
Field values (terms) are lexicographically ordered by unicode value, and numbered starting at 1. *
* 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. *
WARNING: as of Solr 1.4, ord() and rord() can cause excess memory use since they must use a FieldCache entry * at the top level reader, while sorting and function queries now use entries at the segment level. Hence sorting * or using a different function query, in addition to ord()/rord() will double memory use. * @version $Id: OrdFieldSource.java 826531 2009-10-18 21:41:54Z yonik $ */ public class OrdFieldSource extends ValueSource { protected String field; public OrdFieldSource(String field) { this.field = field; } public String description() { return "ord(" + field + ')'; } public DocValues getValues(Map context, IndexReader reader) throws IOException { return new StringIndexDocValues(this, reader, field) { protected String toTerm(String readableValue) { return readableValue; } public float floatVal(int doc) { return (float)order[doc]; } public int intVal(int doc) { return order[doc]; } public long longVal(int doc) { return (long)order[doc]; } public double doubleVal(int doc) { return (double)order[doc]; } public String strVal(int doc) { // the string value of the ordinal, not the string itself return Integer.toString(order[doc]); } public String toString(int doc) { return description() + '=' + intVal(doc); } }; } public boolean equals(Object o) { return o.getClass() == OrdFieldSource.class && this.field.equals(((OrdFieldSource)o).field); } private static final int hcode = OrdFieldSource.class.hashCode(); public int hashCode() { return hcode + field.hashCode(); }; }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy