diff --git a/lucene/core/src/java/org/apache/lucene/index/UninvertingFilterReader.java b/lucene/core/src/java/org/apache/lucene/index/UninvertingFilterReader.java new file mode 100644 index 0000000..ca4d9d8 --- /dev/null +++ b/lucene/core/src/java/org/apache/lucene/index/UninvertingFilterReader.java @@ -0,0 +1,94 @@ +package org.apache.lucene.index; + +/* + * 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. + */ + +import org.apache.lucene.search.FieldCache; + +import java.io.IOException; + +public class UninvertingFilterReader extends FilterAtomicReader { + + /** + * If DocValues are not stored for a given field, the UninvertingFilterReader + * creates them on-the-fly by uninverting the field, and then storing the + * results in the FieldCache + * + * @param in specified base reader. + */ + public UninvertingFilterReader(AtomicReader in) { + super(in); + } + + // TODO -> should we make these caches per-reader, rather than using + // the default FieldCache? + + private static class NumericDocValuesImpl extends NumericDocValues { + + final FieldCache.Longs source; + + NumericDocValuesImpl(FieldCache.Longs source) { + this.source = source; + } + + @Override + public long get(int docID) { + return source.get(docID); + } + } + + @Override + public NumericDocValues getNumericDocValues(String field) throws IOException { + FieldInfo fi = in.getFieldInfos().fieldInfo(field); + if (fi == null) + return null; + if (fi.hasDocValues()) + return super.getNumericDocValues(field); + return new NumericDocValuesImpl(FieldCache.DEFAULT.getLongs(in, field, false)); + } + + @Override + public BinaryDocValues getBinaryDocValues(String field) throws IOException { + FieldInfo fi = in.getFieldInfos().fieldInfo(field); + if (fi == null) + return null; + if (fi.hasDocValues()) + return super.getBinaryDocValues(field); + return FieldCache.DEFAULT.getTerms(in, field); + } + + @Override + public SortedDocValues getSortedDocValues(String field) throws IOException { + FieldInfo fi = in.getFieldInfos().fieldInfo(field); + if (fi == null) + return null; + if (fi.hasDocValues()) + return super.getSortedDocValues(field); + return FieldCache.DEFAULT.getTermsIndex(in, field); + } + + @Override + public SortedSetDocValues getSortedSetDocValues(String field) throws IOException { + FieldInfo fi = in.getFieldInfos().fieldInfo(field); + if (fi == null) + return null; + if (fi.hasDocValues()) + return super.getSortedSetDocValues(field); + return FieldCache.DEFAULT.getDocTermOrds(in, field); + } + +} diff --git a/lucene/core/src/java/org/apache/lucene/search/DocTermOrdsRangeFilter.java b/lucene/core/src/java/org/apache/lucene/search/DocTermOrdsRangeFilter.java index 07e2cba..0f5bf32 100644 --- a/lucene/core/src/java/org/apache/lucene/search/DocTermOrdsRangeFilter.java +++ b/lucene/core/src/java/org/apache/lucene/search/DocTermOrdsRangeFilter.java @@ -16,13 +16,14 @@ package org.apache.lucene.search; * limitations under the License. */ -import java.io.IOException; - import org.apache.lucene.index.AtomicReaderContext; import org.apache.lucene.index.SortedSetDocValues; +import org.apache.lucene.index.UninvertingFilterReader; import org.apache.lucene.util.Bits; import org.apache.lucene.util.BytesRef; +import java.io.IOException; + /** * A range filter built on top of a cached multi-valued term field (in {@link FieldCache}). * @@ -59,7 +60,8 @@ public abstract class DocTermOrdsRangeFilter extends Filter { return new DocTermOrdsRangeFilter(field, lowerVal, upperVal, includeLower, includeUpper) { @Override public DocIdSet getDocIdSet(AtomicReaderContext context, Bits acceptDocs) throws IOException { - final SortedSetDocValues docTermOrds = FieldCache.DEFAULT.getDocTermOrds(context.reader(), field); + //final SortedSetDocValues docTermOrds = FieldCache.DEFAULT.getDocTermOrds(context.reader(), field); + final SortedSetDocValues docTermOrds = new UninvertingFilterReader(context.reader()).getSortedSetDocValues(field); final long lowerPoint = lowerVal == null ? -1 : docTermOrds.lookupTerm(lowerVal); final long upperPoint = upperVal == null ? -1 : docTermOrds.lookupTerm(upperVal);