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

[lucene] branch main updated: Fix Slop Issue in MultiFieldQueryParser (#12196)

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

rmuir 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 96efb34d00b Fix Slop Issue in MultiFieldQueryParser (#12196)
96efb34d00b is described below

commit 96efb34d00bbdac6bdac2fd6f038e1dad1e31195
Author: Jasir KT <me...@gmail.com>
AuthorDate: Wed Mar 8 21:46:09 2023 +0530

    Fix Slop Issue in MultiFieldQueryParser (#12196)
    
    In Lucene 5.4.0 62313b83ba9c69379e1f84dffc881a361713ce9 introduced some changes for immutability of queries. setBoost() function was replaced with new BoostQuery(), but BoostQuery is not handled in setSlop function. This commit adds the handling of BoostQuery in setSlop() function.
---
 lucene/CHANGES.txt                                                   | 2 ++
 .../org/apache/lucene/queryparser/classic/MultiFieldQueryParser.java | 4 ++++
 .../apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java | 5 +++++
 3 files changed, 11 insertions(+)

diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 7391f268757..0a0a265c8b0 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -162,6 +162,8 @@ Bug Fixes
 
 * GITHUB#12158: KeywordField#newSetQuery should clone input BytesRef[] to avoid modifying provided array. (Greg Miller)
 
+* GITHUB#12196: Fix MultiFieldQueryParser to handle both query boost and phrase slop at the same time.  (Jasir KT)
+
 Build
 ---------------------
 
diff --git a/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/MultiFieldQueryParser.java b/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/MultiFieldQueryParser.java
index a269901bb42..eb9abad7813 100644
--- a/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/MultiFieldQueryParser.java
+++ b/lucene/queryparser/src/java/org/apache/lucene/queryparser/classic/MultiFieldQueryParser.java
@@ -124,6 +124,10 @@ public class MultiFieldQueryParser extends QueryParser {
       if (slop != mpq.getSlop()) {
         q = new MultiPhraseQuery.Builder(mpq).setSlop(slop).build();
       }
+    } else if (q instanceof BoostQuery boostQuery) {
+      Query subQuery = boostQuery.getQuery();
+      subQuery = applySlop(subQuery, slop);
+      q = new BoostQuery(subQuery, boostQuery.getBoost());
     }
     return q;
   }
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
index 57ed0e70972..ee684c30f85 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
@@ -156,6 +156,11 @@ public class TestMultiFieldQueryParser extends LuceneTestCase {
     q = mfqp.parse("one AND two AND foo:test");
     assertEquals("+((b:one)^5.0 (t:one)^10.0) +((b:two)^5.0 (t:two)^10.0) +foo:test", q.toString());
 
+    // Check boost with slop
+    // See https://github.com/apache/lucene/issues/12195
+    q = mfqp.parse("\"one two\"~2");
+    assertEquals("(b:\"one two\"~2)^5.0 (t:\"one two\"~2)^10.0", q.toString());
+
     q = mfqp.parse("one^3 AND two^4");
     assertEquals("+((b:one)^5.0 (t:one)^10.0)^3.0 +((b:two)^5.0 (t:two)^10.0)^4.0", q.toString());
   }