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.
This commit is contained in:
Jasir KT 2023-03-08 21:46:09 +05:30 committed by GitHub
parent 0651d25713
commit 96efb34d00
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 0 deletions

View File

@ -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
---------------------

View File

@ -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;
}

View File

@ -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());
}