LUCENE-7957: ConjunctionScorer.getChildren was failing to return all child scorers

This commit is contained in:
Mike McCandless 2017-09-19 10:46:51 -04:00
parent bc95209774
commit cec5b418ea
3 changed files with 60 additions and 3 deletions

View File

@ -65,6 +65,9 @@ Bug Fixes
it did not properly break ties on the surface forms when both the weights and
the 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 first

View File

@ -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 {
@Override
public Collection<ChildScorer> getChildren() {
ArrayList<ChildScorer> children = new ArrayList<>();
for (Scorer scorer : scorers) {
for (Scorer scorer : required) {
children.add(new ChildScorer(scorer, "MUST"));
}
return children;

View File

@ -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);
}
}