1 package org.apache.lucene.search.function;
4 * Licensed to the Apache Software Foundation (ASF) under one or more
5 * contributor license agreements. See the NOTICE file distributed with
6 * this work for additional information regarding copyright ownership.
7 * The ASF licenses this file to You under the Apache License, Version 2.0
8 * (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
20 import org.apache.lucene.index.IndexReader;
21 import org.apache.lucene.search.FieldCache;
22 import org.apache.lucene.search.function.DocValues;
24 import java.io.IOException;
27 * Expert: obtains float field values from the
28 * {@link org.apache.lucene.search.FieldCache FieldCache}
29 * using <code>getFloats()</code> and makes those values
30 * available as other numeric types, casting as needed.
32 * @lucene.experimental
34 * @see org.apache.lucene.search.function.FieldCacheSource for requirements
37 * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
38 * searching, if {@link #getValues} is invoked with a
39 * composite (multi-segment) reader, this can easily cause
40 * double RAM usage for the values in the FieldCache. It's
41 * best to switch your application to pass only atomic
42 * (single segment) readers to this API.</p>
44 public class FloatFieldSource extends FieldCacheSource {
45 private FieldCache.FloatParser parser;
48 * Create a cached float field source with default string-to-float parser.
50 public FloatFieldSource(String field) {
55 * Create a cached float field source with a specific string-to-float parser.
57 public FloatFieldSource(String field, FieldCache.FloatParser parser) {
62 /*(non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
64 public String description() {
65 return "float(" + super.description() + ')';
68 /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#getCachedValues(org.apache.lucene.search.FieldCache, java.lang.String, org.apache.lucene.index.IndexReader) */
70 public DocValues getCachedFieldValues (FieldCache cache, String field, IndexReader reader) throws IOException {
71 final float[] arr = cache.getFloats(reader, field, parser);
72 return new DocValues() {
73 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#floatVal(int) */
75 public float floatVal(int doc) {
78 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#toString(int) */
80 public String toString(int doc) {
81 return description() + '=' + arr[doc];
83 /*(non-Javadoc) @see org.apache.lucene.search.function.DocValues#getInnerArray() */
85 Object getInnerArray() {
91 /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceEquals(org.apache.lucene.search.function.FieldCacheSource) */
93 public boolean cachedFieldSourceEquals(FieldCacheSource o) {
94 if (o.getClass() != FloatFieldSource.class) {
97 FloatFieldSource other = (FloatFieldSource)o;
98 return this.parser==null ?
100 this.parser.getClass() == other.parser.getClass();
103 /*(non-Javadoc) @see org.apache.lucene.search.function.FieldCacheSource#cachedFieldSourceHashCode() */
105 public int cachedFieldSourceHashCode() {
106 return parser==null ?
107 Float.class.hashCode() : parser.getClass().hashCode();