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:21:28 UTC
[lucene] 01/02: Fix Slop Issue in MultiFieldQueryParser (#12196)
This is an automated email from the ASF dual-hosted git repository.
rmuir pushed a commit to branch branch_9x
in repository https://gitbox.apache.org/repos/asf/lucene.git
commit 9f2addc2d530ef86cd4b05fe217f5a682fe5903c
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 193f1731626..be26b11490f 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -63,6 +63,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());
}