Skip to content
Open
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
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?

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 qpm = map.get("f").subMap.get("a");
assertEquals(0, qpm.boost, 0.0);
QueryPhraseMap qpm1 = qpm.subMap.get("b");
assertEquals(20, qpm1.boost, 0.0);
QueryPhraseMap qpm2 = qpm1.subMap.get("c");
// make sure final boost is from the query and not the expanded boost 1
assertEquals(100, qpm2.boost, 0.0);

QueryPhraseMap qpm3 = map.get("f").subMap.get("b");
assertEquals(0, qpm3.boost, 0.0);
QueryPhraseMap qpm4 = qpm3.subMap.get("c");
assertEquals(50, qpm4.boost, 0.0);
}

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

Expand Down
Loading