You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by ro...@apache.org on 2023/01/16 11:47:58 UTC

[lucene] branch main updated: Don't throw UOE when highlighting FieldExistsQuery (#12088)

This is an automated email from the ASF dual-hosted git repository.

romseygeek pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/lucene.git


The following commit(s) were added to refs/heads/main by this push:
     new fc7b937affe Don't throw UOE when highlighting FieldExistsQuery (#12088)
fc7b937affe is described below

commit fc7b937affe09d8b95c2ecbcfae072ec8a39065d
Author: Alan Woodward <ro...@apache.org>
AuthorDate: Mon Jan 16 11:47:51 2023 +0000

    Don't throw UOE when highlighting FieldExistsQuery (#12088)
    
    WeightedSpanTermExtractor will try to rewrite queries that it doesn't
    know about, to see if they end up as something it does know about and
    that it can extract terms from. To support field merging, it rewrites against
    a delegating leaf reader that does not support getFieldInfos().
    
    FieldExistsQuery uses getFieldInfos() in its rewrite, which means that
    if one is passed to WeightedSpanTermExtractor, we get an
    UnsupportedOperationException thrown.
    
    This commit makes WeightedSpanTermExtractor aware of FieldExistsQuery,
    so that it can just ignore it and avoid throwing an exception.
---
 lucene/CHANGES.txt                                           |  3 +++
 .../lucene/search/highlight/WeightedSpanTermExtractor.java   |  3 +++
 .../org/apache/lucene/search/highlight/TestHighlighter.java  | 12 ++++++++++++
 3 files changed, 18 insertions(+)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index b5a0c47eff0..d1252ba96c1 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -240,6 +240,9 @@ Bug Fixes
 * GITHUB#12072: Fix exponential runtime for nested BooleanQuery#rewrite when a
   BooleanClause is non-scoring. (Ben Trent)
 
+* GITHUB#12088: WeightedSpanTermExtractor should not throw UnsupportedOperationException
+  when it encounters a FieldExistsQuery. (Alan Woodward)
+
 Optimizations
 ---------------------
 * GITHUB#11738: Optimize MultiTermQueryConstantScoreWrapper when a term is present that matches all
diff --git a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
index b0c7d9c6ab4..9fd09f0eb4d 100644
--- a/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
+++ b/lucene/highlighter/src/java/org/apache/lucene/search/highlight/WeightedSpanTermExtractor.java
@@ -53,6 +53,7 @@ import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.BoostQuery;
 import org.apache.lucene.search.ConstantScoreQuery;
 import org.apache.lucene.search.DisjunctionMaxQuery;
+import org.apache.lucene.search.FieldExistsQuery;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.MatchAllDocsQuery;
 import org.apache.lucene.search.MultiPhraseQuery;
@@ -232,6 +233,8 @@ public class WeightedSpanTermExtractor {
       }
     } else if (query instanceof MatchAllDocsQuery) {
       // nothing
+    } else if (query instanceof FieldExistsQuery) {
+      // nothing
     } else if (query instanceof FunctionScoreQuery) {
       extract(((FunctionScoreQuery) query).getWrappedQuery(), boost, terms);
     } else if (isQueryUnsupported(query.getClass())) {
diff --git a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/TestHighlighter.java b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/TestHighlighter.java
index b8bd645935e..299a8e850bc 100644
--- a/lucene/highlighter/src/test/org/apache/lucene/search/highlight/TestHighlighter.java
+++ b/lucene/highlighter/src/test/org/apache/lucene/search/highlight/TestHighlighter.java
@@ -63,6 +63,7 @@ import org.apache.lucene.search.BooleanClause.Occur;
 import org.apache.lucene.search.BooleanQuery;
 import org.apache.lucene.search.ConstantScoreQuery;
 import org.apache.lucene.search.DoubleValuesSource;
+import org.apache.lucene.search.FieldExistsQuery;
 import org.apache.lucene.search.FuzzyQuery;
 import org.apache.lucene.search.IndexSearcher;
 import org.apache.lucene.search.MultiPhraseQuery;
@@ -1424,6 +1425,17 @@ public class TestHighlighter extends BaseTokenStreamTestCase implements Formatte
         "foo"); // field "foo"
   }
 
+  public void testFieldExistsQuery() throws IOException {
+    Query q =
+        new BooleanQuery.Builder()
+            .add(new TermQuery(new Term("field", "term")), Occur.MUST)
+            .add(new FieldExistsQuery("field"), Occur.MUST)
+            .build();
+
+    WeightedSpanTermExtractor extractor = new WeightedSpanTermExtractor();
+    extractor.getWeightedSpanTerms(q, 1, new CannedTokenStream(new Token("term", 0, 4)), "field");
+  }
+
   public void testGetBestSingleFragmentWithWeights() throws Exception {
 
     TestHighlightRunner helper =