diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt index 5192fbfe6d6..47e23d6fd34 100644 --- a/lucene/CHANGES.txt +++ b/lucene/CHANGES.txt @@ -127,6 +127,9 @@ Bug Fixes * LUCENE-8849: DocValuesRewriteMethod.visit wasn't visiting its embedded query (Michele Palmia, David Smiley) +* LUCENE-9258: DocTermsIndexDocValues assumed it was operating on a SortedDocValues (single valued) field when + it could be multi-valued used with a SortedSetSelector (Michele Palmia) + Other --------------------- diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/docvalues/DocTermsIndexDocValues.java b/lucene/queries/src/java/org/apache/lucene/queries/function/docvalues/DocTermsIndexDocValues.java index 1cc79a4b3fe..6c3b3b8b1c5 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/docvalues/DocTermsIndexDocValues.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/docvalues/DocTermsIndexDocValues.java @@ -40,15 +40,13 @@ public abstract class DocTermsIndexDocValues extends FunctionValues { protected final ValueSource vs; protected final MutableValueStr val = new MutableValueStr(); protected final CharsRefBuilder spareChars = new CharsRefBuilder(); - private final String field; private int lastDocID; public DocTermsIndexDocValues(ValueSource vs, LeafReaderContext context, String field) throws IOException { - this(field, vs, open(context, field)); + this(vs, open(context, field)); } - - protected DocTermsIndexDocValues(String field, ValueSource vs, SortedDocValues termsIndex) { - this.field = field; + + protected DocTermsIndexDocValues(ValueSource vs, SortedDocValues termsIndex) { this.vs = vs; this.termsIndex = termsIndex; } @@ -145,23 +143,11 @@ public abstract class DocTermsIndexDocValues extends FunctionValues { final int uu = upper; return new ValueSourceScorer(weight, readerContext, this) { - final SortedDocValues values = readerContext.reader().getSortedDocValues(field); - private int lastDocID; - @Override public boolean matches(int doc) throws IOException { - if (doc < lastDocID) { - throw new IllegalArgumentException("docs were sent out-of-order: lastDocID=" + lastDocID + " vs docID=" + doc); - } - if (doc > values.docID()) { - values.advance(doc); - } - if (doc == values.docID()) { - int ord = values.ordValue(); - return ord >= ll && ord <= uu; - } else { - return false; - } + if (!exists(doc)) return false; + float docVal = ordVal(doc); + return docVal >= ll && docVal <= uu; } }; } diff --git a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SortedSetFieldSource.java b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SortedSetFieldSource.java index d32cc3bc116..ddf71292b0c 100644 --- a/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SortedSetFieldSource.java +++ b/lucene/queries/src/java/org/apache/lucene/queries/function/valuesource/SortedSetFieldSource.java @@ -58,7 +58,7 @@ public class SortedSetFieldSource extends FieldCacheSource { public FunctionValues getValues(Map context, LeafReaderContext readerContext) throws IOException { SortedSetDocValues sortedSet = DocValues.getSortedSet(readerContext.reader(), field); SortedDocValues view = SortedSetSelector.wrap(sortedSet, selector); - return new DocTermsIndexDocValues(this.field, this, view) { + return new DocTermsIndexDocValues(this, view) { @Override protected String toTerm(String readableValue) { return readableValue; diff --git a/lucene/queries/src/test/org/apache/lucene/queries/function/TestSortedSetFieldSource.java b/lucene/queries/src/test/org/apache/lucene/queries/function/TestSortedSetFieldSource.java index b72bfebfa44..84dd263dd5e 100644 --- a/lucene/queries/src/test/org/apache/lucene/queries/function/TestSortedSetFieldSource.java +++ b/lucene/queries/src/test/org/apache/lucene/queries/function/TestSortedSetFieldSource.java @@ -25,9 +25,7 @@ import org.apache.lucene.index.LeafReader; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.queries.function.valuesource.SortedSetFieldSource; -import org.apache.lucene.search.IndexSearcher; -import org.apache.lucene.search.SortField; -import org.apache.lucene.search.SortedSetSortField; +import org.apache.lucene.search.*; import org.apache.lucene.store.Directory; import org.apache.lucene.util.BytesRef; import org.apache.lucene.util.LuceneTestCase; @@ -66,7 +64,16 @@ public class TestSortedSetFieldSource extends LuceneTestCase { vssf = vssf.rewrite(searcher); sf = sf.rewrite(searcher); assertEquals(sf, vssf); - + + // test scorer + vs = new SortedSetFieldSource("value"); + values = vs.getValues(Collections.emptyMap(), ar.getContext()); + ValueSourceScorer vss = values.getRangeScorer(new MatchAllDocsQuery().createWeight(searcher, ScoreMode.TOP_SCORES, 1), ar.getContext(), "a", "z", true, true); + + DocIdSetIterator iterator = vss.iterator(); + assertEquals("baz", values.strVal(iterator.nextDoc())); + assertEquals("bar", values.strVal(iterator.nextDoc())); + ir.close(); dir.close(); }