Skip to content

Fix npe when using source confirmed text query against missing field #127414

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions docs/changelog/127414.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
pr: 127414
summary: Fix npe when using source confirmed text query against missing field
area: Search
type: bug
issues: []
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,11 @@ public boolean isCacheable(LeafReaderContext ctx) {
@Override
public Explanation explain(LeafReaderContext context, int doc) throws IOException {
NumericDocValues norms = context.reader().getNormValues(field);
RuntimePhraseScorer scorer = (RuntimePhraseScorer) scorerSupplier(context).get(0);
ScorerSupplier scorerSupplier = scorerSupplier(context);
if (scorerSupplier == null) {
return Explanation.noMatch("No matching phrase");
}
RuntimePhraseScorer scorer = (RuntimePhraseScorer) scorerSupplier.get(0);
if (scorer == null) {
return Explanation.noMatch("No matching phrase");
}
Expand All @@ -277,6 +281,7 @@ public Explanation explain(LeafReaderContext context, int doc) throws IOExceptio
}
float phraseFreq = scorer.freq();
Explanation freqExplanation = Explanation.match(phraseFreq, "phraseFreq=" + phraseFreq);
assert simScorer != null;
Explanation scoreExplanation = simScorer.explain(freqExplanation, getNormValue(norms, doc));
return Explanation.match(
scoreExplanation.getValue(),
Expand Down Expand Up @@ -321,7 +326,11 @@ public Matches matches(LeafReaderContext context, int doc) throws IOException {
Weight innerWeight = in.createWeight(searcher, ScoreMode.COMPLETE_NO_SCORES, 1);
return innerWeight.matches(context, doc);
}
RuntimePhraseScorer scorer = (RuntimePhraseScorer) scorerSupplier(context).get(0L);
ScorerSupplier scorerSupplier = scorerSupplier(context);
if (scorerSupplier == null) {
return null;
}
RuntimePhraseScorer scorer = (RuntimePhraseScorer) scorerSupplier.get(0L);
if (scorer == null) {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.lucene.queries.spans.SpanTermQuery;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.Explanation;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.apache.lucene.search.Matches;
Expand Down Expand Up @@ -101,6 +102,26 @@ public void testTerm() throws Exception {
}
}

public void testMissingPhrase() throws Exception {
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(Lucene.STANDARD_ANALYZER))) {

Document doc = new Document();
doc.add(new TextField("body", "a b c b a b c", Store.YES));
w.addDocument(doc);

try (IndexReader reader = DirectoryReader.open(w)) {
IndexSearcher searcher = newSearcher(reader);
PhraseQuery query = new PhraseQuery("missing_field", "b", "c");
Query sourceConfirmedPhraseQuery = new SourceConfirmedTextQuery(query, SOURCE_FETCHER_PROVIDER, Lucene.STANDARD_ANALYZER);
Explanation explanation = searcher.explain(sourceConfirmedPhraseQuery, 0);
assertFalse(explanation.isMatch());

Weight weight = searcher.createWeight(query, ScoreMode.COMPLETE, 1);
assertNull(weight.matches(getOnlyLeafReader(reader).getContext(), 0));
}
}
}

public void testPhrase() throws Exception {
try (Directory dir = newDirectory(); IndexWriter w = new IndexWriter(dir, newIndexWriterConfig(Lucene.STANDARD_ANALYZER))) {

Expand Down
Loading