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 java.io.IOException;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.search.FieldCache;
26 * Expert: A base class for ValueSource implementations that retrieve values for
27 * a single field from the {@link org.apache.lucene.search.FieldCache FieldCache}.
29 * Fields used herein must be indexed (doesn't matter if these fields are stored or not).
31 * It is assumed that each such indexed field is untokenized, or at least has a single token in a document.
32 * For documents with multiple tokens of the same field, behavior is undefined (It is likely that current
33 * code would use the value of one of these tokens, but this is not guaranteed).
35 * Document with no tokens in this field are assigned the <code>Zero</code> value.
37 * @lucene.experimental
39 * <p><b>NOTE</b>: with the switch in 2.9 to segment-based
40 * searching, if {@link #getValues} is invoked with a
41 * composite (multi-segment) reader, this can easily cause
42 * double RAM usage for the values in the FieldCache. It's
43 * best to switch your application to pass only atomic
44 * (single segment) readers to this API.</p>
46 public abstract class FieldCacheSource extends ValueSource {
50 * Create a cached field source for the input field.
52 public FieldCacheSource(String field) {
56 /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#getValues(org.apache.lucene.index.IndexReader) */
58 public final DocValues getValues(IndexReader reader) throws IOException {
59 return getCachedFieldValues(FieldCache.DEFAULT, field, reader);
62 /* (non-Javadoc) @see org.apache.lucene.search.function.ValueSource#description() */
64 public String description() {
69 * Return cached DocValues for input field and reader.
70 * @param cache FieldCache so that values of a field are loaded once per reader (RAM allowing)
71 * @param field Field for which values are required.
74 public abstract DocValues getCachedFieldValues(FieldCache cache, String field, IndexReader reader) throws IOException;
76 /*(non-Javadoc) @see java.lang.Object#equals(java.lang.Object) */
78 public final boolean equals(Object o) {
79 if (!(o instanceof FieldCacheSource)) {
82 FieldCacheSource other = (FieldCacheSource) o;
84 this.field.equals(other.field) &&
85 cachedFieldSourceEquals(other);
88 /*(non-Javadoc) @see java.lang.Object#hashCode() */
90 public final int hashCode() {
93 cachedFieldSourceHashCode();
97 * Check if equals to another {@link FieldCacheSource}, already knowing that cache and field are equal.
98 * @see Object#equals(java.lang.Object)
100 public abstract boolean cachedFieldSourceEquals(FieldCacheSource other);
103 * Return a hash code of a {@link FieldCacheSource}, without the hash-codes of the field
104 * and the cache (those are taken care of elsewhere).
105 * @see Object#hashCode()
107 public abstract int cachedFieldSourceHashCode();