Skip to content
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
2 changes: 2 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ Bug Fixes

* GITHUB#15125: Handle inconsistent schema on flush with index sorts (Nhat Nguyen)

* GITHUB#15434: Don't override boost of conflicting expanded phrases by taking the max boost in markTerminal for FVHighlighter (Luana Fragoso)

Changes in Runtime Behavior
---------------------
* GITHUB#14187: The query cache is now disabled by default. (Adrien Grand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ private void markTerminal(float boost) {
private void markTerminal(int slop, float boost) {
this.terminal = true;
this.slop = slop;
this.boost = boost;
this.boost = Math.max(this.boost, boost);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your approach seems simpler, but I think we have an opportunity to optimize this. Instead of always executing all assignments, should we have something like:

   if (!this.terminal || boost > this.boost) {
        this.terminal = true;
        this.slop = slop;
        this.boost = boost;
        this.termOrPhraseNumber = fieldQuery.nextTermOrPhraseNumber();
      }

Benefits:

Performance: Avoids unnecessary state updates when boost doesn't improve

Semantic correctness: termOrPhraseNumber only increments when meaningful changes occur

Cleaner logic: Single condition handles both initialization and duplicate prevention

Current approach with Math.max():

Always updates all fields, even when boost=1 < existing boost=100

Increments termOrPhraseNumber unnecessarily on duplicate calls

Proposed approach:

Only updates when first call (!this.terminal) or when boost improves (boost > this.boost)

More efficient for queries with many overlapping phrases

What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's a fine suggestion but it's also a trivial matter. I'd even argue the complexity of the condition you propose makes the situation worse.... a future reader may scratching their heads for a little longer than merits the code being avoided.

this.termOrPhraseNumber = fieldQuery.nextTermOrPhraseNumber();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -814,6 +815,48 @@ public void testQueryPhraseMapOverlap2gram() throws Exception {
assertEquals(0, qpm2.subMap.size());
}

public void testQueryPhraseMapDuplicate() throws IOException {
BooleanQuery.Builder query = new BooleanQuery.Builder();
Query bq = toPhraseQuery(analyze("a b c", F, analyzerB), F);
bq = new BoostQuery(bq, 100);
query.add(bq, Occur.SHOULD);

bq = toPhraseQuery(analyze("a b", F, analyzerB), F);
bq = new BoostQuery(bq, 20);
query.add(bq, Occur.SHOULD);

bq = toPhraseQuery(analyze("b c", F, analyzerB), F);
bq = new BoostQuery(bq, 50);
query.add(bq, Occur.SHOULD);

bq = query.build();
FieldQuery fq = new FieldQuery(bq, true, true);
Set<Query> flatQueries = new LinkedHashSet<>();
fq.flatten(bq, searcher, flatQueries, 1f);

assertCollectionQueries(
fq.expand(flatQueries),
pqF(100, "a", "b", "c"),
pqF(20, "a", "b"),
// "a b c": 1 -> expanded from "a b" + "b c"
new BoostQuery(pqF(1f, "a", "b", "c"), 1f),
pqF(50, "b", "c"));

Map<String, QueryPhraseMap> map = fq.rootMaps;
QueryPhraseMap a_qpm = map.get("f").subMap.get("a");
assertEquals(0, a_qpm.boost, 0.0);
QueryPhraseMap b_qpm = a_qpm.subMap.get("b");
assertEquals(20, b_qpm.boost, 0.0);
QueryPhraseMap c_qpm = b_qpm.subMap.get("c");
// make sure final boost is from the query and not the expanded boost 1
assertEquals(100, c_qpm.boost, 0.0);

b_qpm = map.get("f").subMap.get("b");
assertEquals(0, b_qpm.boost, 0.0);
c_qpm = b_qpm.subMap.get("c");
assertEquals(50, c_qpm.boost, 0.0);
}

public void testSearchPhrase() throws Exception {
Query query = pqF("a", "b", "c");

Expand Down