mirror of https://github.com/apache/lucene.git
LUCENE-9524: Fix NPE in SpanWeight#explain when no scoring is require… (#1978)
This commit is contained in:
parent
744934c826
commit
6990109f9b
|
@ -161,6 +161,9 @@ Bug fixes
|
||||||
* LUCENE-9365: FuzzyQuery was missing matches when prefix length was equal to the term length
|
* LUCENE-9365: FuzzyQuery was missing matches when prefix length was equal to the term length
|
||||||
(Mark Harwood, Mike Drob)
|
(Mark Harwood, Mike Drob)
|
||||||
|
|
||||||
|
* LUCENE-9524: Fix NPE in SpanWeight#explain when no scoring is required and SpanWeight has null Similarity.SimScorer.
|
||||||
|
(Zach Chen)
|
||||||
|
|
||||||
Other
|
Other
|
||||||
|
|
||||||
* LUCENE-9312: Allow gradle builds against arbitrary JVMs. (Tomoko Uchida, Dawid Weiss)
|
* LUCENE-9312: Allow gradle builds against arbitrary JVMs. (Tomoko Uchida, Dawid Weiss)
|
||||||
|
|
|
@ -20,6 +20,7 @@ package org.apache.lucene.search.spans;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Comparator;
|
import java.util.Comparator;
|
||||||
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.lucene.index.LeafReaderContext;
|
import org.apache.lucene.index.LeafReaderContext;
|
||||||
|
@ -156,13 +157,19 @@ public abstract class SpanWeight extends Weight {
|
||||||
if (scorer != null) {
|
if (scorer != null) {
|
||||||
int newDoc = scorer.iterator().advance(doc);
|
int newDoc = scorer.iterator().advance(doc);
|
||||||
if (newDoc == doc) {
|
if (newDoc == doc) {
|
||||||
float freq = scorer.sloppyFreq();
|
if (simScorer != null) {
|
||||||
LeafSimScorer docScorer = new LeafSimScorer(simScorer, context.reader(), field, true);
|
float freq = scorer.sloppyFreq();
|
||||||
Explanation freqExplanation = Explanation.match(freq, "phraseFreq=" + freq);
|
LeafSimScorer docScorer = new LeafSimScorer(simScorer, context.reader(), field, true);
|
||||||
Explanation scoreExplanation = docScorer.explain(doc, freqExplanation);
|
Explanation freqExplanation = Explanation.match(freq, "phraseFreq=" + freq);
|
||||||
return Explanation.match(scoreExplanation.getValue(),
|
Explanation scoreExplanation = docScorer.explain(doc, freqExplanation);
|
||||||
"weight("+getQuery()+" in "+doc+") [" + similarity.getClass().getSimpleName() + "], result of:",
|
return Explanation.match(scoreExplanation.getValue(),
|
||||||
scoreExplanation);
|
"weight("+getQuery()+" in "+doc+") [" + similarity.getClass().getSimpleName() + "], result of:",
|
||||||
|
scoreExplanation);
|
||||||
|
} else {
|
||||||
|
// simScorer won't be set when scoring isn't needed
|
||||||
|
return Explanation.match(0f, String.format(Locale.ROOT,
|
||||||
|
"match %s in %s without score", getQuery(), doc));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -17,12 +17,22 @@
|
||||||
package org.apache.lucene.search.spans;
|
package org.apache.lucene.search.spans;
|
||||||
|
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import org.apache.lucene.document.Document;
|
||||||
|
import org.apache.lucene.document.Field;
|
||||||
|
import org.apache.lucene.index.DirectoryReader;
|
||||||
|
import org.apache.lucene.index.IndexWriter;
|
||||||
|
import org.apache.lucene.index.LeafReaderContext;
|
||||||
|
import org.apache.lucene.index.Term;
|
||||||
import org.apache.lucene.search.*;
|
import org.apache.lucene.search.*;
|
||||||
|
import org.apache.lucene.store.Directory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TestExplanations subclass focusing on span queries
|
* TestExplanations subclass focusing on span queries
|
||||||
*/
|
*/
|
||||||
public class TestSpanExplanations extends BaseExplanationTestCase {
|
public class TestSpanExplanations extends BaseExplanationTestCase {
|
||||||
|
private static final String FIELD_CONTENT = "content";
|
||||||
|
|
||||||
/* simple SpanTermQueries */
|
/* simple SpanTermQueries */
|
||||||
|
|
||||||
|
@ -165,4 +175,32 @@ public class TestSpanExplanations extends BaseExplanationTestCase {
|
||||||
qtest(q, new int[] {0,1,3});
|
qtest(q, new int[] {0,1,3});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testExplainWithoutScoring() throws IOException {
|
||||||
|
SpanNearQuery query = new SpanNearQuery(new SpanQuery[]{
|
||||||
|
new SpanTermQuery(new Term(FIELD_CONTENT, "dolor")),
|
||||||
|
new SpanTermQuery(new Term(FIELD_CONTENT, "lorem"))},
|
||||||
|
0,
|
||||||
|
true);
|
||||||
|
|
||||||
|
try (Directory rd = newDirectory()) {
|
||||||
|
try (IndexWriter writer = new IndexWriter(rd, newIndexWriterConfig(analyzer))) {
|
||||||
|
Document doc = new Document();
|
||||||
|
doc.add(newTextField(FIELD_CONTENT, "dolor lorem ipsum", Field.Store.YES));
|
||||||
|
writer.addDocument(doc);
|
||||||
|
}
|
||||||
|
|
||||||
|
try (DirectoryReader reader = DirectoryReader.open(rd)) {
|
||||||
|
IndexSearcher indexSearcher = newSearcher(reader);
|
||||||
|
SpanWeight spanWeight = query.createWeight(indexSearcher, ScoreMode.COMPLETE_NO_SCORES, 1f);
|
||||||
|
|
||||||
|
final LeafReaderContext ctx = reader.leaves().get(0);
|
||||||
|
Explanation explanation = spanWeight.explain(ctx, 0);
|
||||||
|
|
||||||
|
assertEquals(0f, explanation.getValue());
|
||||||
|
assertEquals("match spanNear([content:dolor, content:lorem], 0, true) in 0 without score",
|
||||||
|
explanation.getDescription());
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue