Skip to content

Commit 6d6da7c

Browse files
authored
Fix merging logic of Suggester Options (#29514)
Suggester Options have a collate match field that is returned when the prune option is set to true. These values should be merged together in the query reduce phase, otherwise good suggestions that result in rare hits in shards with results that do not arrive first may be incorrectly marked as not matching the collate query.
1 parent 383856a commit 6d6da7c

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

server/src/main/java/org/elasticsearch/search/suggest/Suggest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,13 @@ public static Option fromXContent(XContentParser parser) {
742742

743743
protected void mergeInto(Option otherOption) {
744744
score = Math.max(score, otherOption.score);
745+
if (otherOption.collateMatch != null) {
746+
if (collateMatch == null) {
747+
collateMatch = otherOption.collateMatch;
748+
} else {
749+
collateMatch |= otherOption.collateMatch;
750+
}
751+
}
745752
}
746753

747754
@Override

server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,5 +191,27 @@ public void testParsingExceptionOnUnknownSuggestion() throws IOException {
191191
}
192192
}
193193

194-
194+
public void testMergingSuggestionOptions() {
195+
String suggestedWord = randomAlphaOfLength(10);
196+
String secondWord = randomAlphaOfLength(10);
197+
Text suggestionText = new Text(suggestedWord + " " + secondWord);
198+
Text highlighted = new Text("<em>" + suggestedWord + "</em> " + secondWord);
199+
PhraseSuggestion.Entry.Option option1 = new Option(suggestionText, highlighted, 0.7f, false);
200+
PhraseSuggestion.Entry.Option option2 = new Option(suggestionText, highlighted, 0.8f, true);
201+
PhraseSuggestion.Entry.Option option3 = new Option(suggestionText, highlighted, 0.6f);
202+
assertEquals(suggestionText, option1.getText());
203+
assertEquals(highlighted, option1.getHighlighted());
204+
assertFalse(option1.collateMatch());
205+
assertTrue(option1.getScore() > 0.6f);
206+
option1.mergeInto(option2);
207+
assertEquals(suggestionText, option1.getText());
208+
assertEquals(highlighted, option1.getHighlighted());
209+
assertTrue(option1.collateMatch());
210+
assertTrue(option1.getScore() > 0.7f);
211+
option1.mergeInto(option3);
212+
assertEquals(suggestionText, option1.getText());
213+
assertEquals(highlighted, option1.getHighlighted());
214+
assertTrue(option1.getScore() > 0.7f);
215+
assertTrue(option1.collateMatch());
216+
}
195217
}

0 commit comments

Comments
 (0)