Apache Lucene open-source search software
LUCENE-7957: ConjunctionScorer.getChildren was failing to return all child scorers
Mike McCandlesscec5b418ea0b
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex 5a95d6c04b1..84880ab64fc 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -65,6 +65,9 @@ Bug Fixesit did not properly break ties on the surface forms when both the weights andthe analyzed forms were equal. (Robert Muir)+* LUCENE-7957: ConjunctionScorer.getChildren was failing to return all+ child scorers (Adrien Grand, Mike McCandless)+Build* SOLR-11181: Switch order of maven artifact publishing procedure: deploy firstdiff --git a/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java b/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.javaindex 9cddab884c6..6ad4f403974 100644--- a/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java+++ b/lucene/core/src/java/org/apache/lucene/search/ConjunctionScorer.java@@ -26,6 +26,7 @@ class ConjunctionScorer extends Scorer {final DocIdSetIterator disi;final Scorer[] scorers;+ final Collection<Scorer> required;/** Create a new {@link ConjunctionScorer}, note that {@code scorers} must be a subset of {@code required}. */ConjunctionScorer(Weight weight, Collection<Scorer> required, Collection<Scorer> scorers) {@@ -33,6 +34,7 @@ class ConjunctionScorer extends Scorer {assert required.containsAll(scorers);this.disi = ConjunctionDISI.intersectScorers(required);this.scorers = scorers.toArray(new Scorer[scorers.size()]);+ this.required = required;}@Override@@ -67,7 +69,7 @@ class ConjunctionScorer extends Scorer {@Overridepublic Collection<ChildScorer> getChildren() {ArrayList<ChildScorer> children = new ArrayList<>();- for (Scorer scorer : scorers) {+ for (Scorer scorer : required) {children.add(new ChildScorer(scorer, "MUST"));}return children;diff --git a/lucene/core/src/test/org/apache/lucene/search/TestConjunctions.java b/lucene/core/src/test/org/apache/lucene/search/TestConjunctions.javaindex 8b261211ae2..de18dda470b 100644--- a/lucene/core/src/test/org/apache/lucene/search/TestConjunctions.java+++ b/lucene/core/src/test/org/apache/lucene/search/TestConjunctions.java@@ -18,22 +18,29 @@ package org.apache.lucene.search;import java.io.IOException;+import java.util.Collection;+import java.util.HashSet;+import java.util.Set;import org.apache.lucene.analysis.Analyzer;import org.apache.lucene.analysis.MockAnalyzer;import org.apache.lucene.document.Document;+import org.apache.lucene.document.Field.Store;+import org.apache.lucene.document.Field;import org.apache.lucene.document.StringField;import org.apache.lucene.document.TextField;-import org.apache.lucene.document.Field.Store;-import org.apache.lucene.index.LeafReaderContext;+import org.apache.lucene.index.DirectoryReader;import org.apache.lucene.index.FieldInvertState;import org.apache.lucene.index.IndexReader;+import org.apache.lucene.index.IndexWriter;import org.apache.lucene.index.IndexWriterConfig;+import org.apache.lucene.index.LeafReaderContext;import org.apache.lucene.index.RandomIndexWriter;import org.apache.lucene.index.Term;import org.apache.lucene.search.similarities.Similarity;import org.apache.lucene.store.Directory;import org.apache.lucene.util.BytesRef;+import org.apache.lucene.util.IOUtils;import org.apache.lucene.util.LuceneTestCase;public class TestConjunctions extends LuceneTestCase {@@ -119,4 +126,49 @@ public class TestConjunctions extends LuceneTestCase {};}}++ public void testScorerGetChildren() throws Exception {+ Directory dir = newDirectory();+ IndexWriter w = new IndexWriter(dir, newIndexWriterConfig());+ Document doc = new Document();+ doc.add(newTextField("field", "a b", Field.Store.NO));+ w.addDocument(doc);+ IndexReader r = DirectoryReader.open(w);+ BooleanQuery.Builder b = new BooleanQuery.Builder();+ b.add(new TermQuery(new Term("field", "a")), BooleanClause.Occur.MUST);+ b.add(new TermQuery(new Term("field", "b")), BooleanClause.Occur.FILTER);+ Query q = b.build();+ IndexSearcher s = new IndexSearcher(r);+ final boolean[] setScorerCalled = new boolean[1];+ s.search(q, new SimpleCollector() {+ @Override+ public void setScorer(Scorer s) throws IOException {+ Collection<Scorer.ChildScorer> childScorers = s.getChildren();+ setScorerCalled[0] = true;+ assertEquals(2, childScorers.size());+ Set<String> terms = new HashSet<>();+ for (Scorer.ChildScorer childScorer : childScorers) {+ Query query = childScorer.child.getWeight().getQuery();+ assertTrue(query instanceof TermQuery);+ Term term = ((TermQuery) query).getTerm();+ assertEquals("field", term.field());+ terms.add(term.text());+ }+ assertEquals(2, terms.size());+ assertTrue(terms.contains("a"));+ assertTrue(terms.contains("b"));+ }++ @Override+ public void collect(int doc) {+ }++ @Override+ public boolean needsScores() {+ return true;+ }+ });+ assertTrue(setScorerCalled[0]);+ IOUtils.close(r, w, dir);+ }}