diff --git a/server/src/main/java/org/elasticsearch/search/suggest/Suggest.java b/server/src/main/java/org/elasticsearch/search/suggest/Suggest.java index a54f1193df008..e40991933592f 100644 --- a/server/src/main/java/org/elasticsearch/search/suggest/Suggest.java +++ b/server/src/main/java/org/elasticsearch/search/suggest/Suggest.java @@ -742,6 +742,13 @@ public static Option fromXContent(XContentParser parser) { protected void mergeInto(Option otherOption) { score = Math.max(score, otherOption.score); + if (otherOption.collateMatch != null) { + if (collateMatch == null) { + collateMatch = otherOption.collateMatch; + } else { + collateMatch |= otherOption.collateMatch; + } + } } @Override diff --git a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java index d54fa0f705f0d..412dd3c6a2017 100644 --- a/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java +++ b/server/src/test/java/org/elasticsearch/search/suggest/SuggestTests.java @@ -191,5 +191,27 @@ public void testParsingExceptionOnUnknownSuggestion() throws IOException { } } - + public void testMergingSuggestionOptions() { + String suggestedWord = randomAlphaOfLength(10); + String secondWord = randomAlphaOfLength(10); + Text suggestionText = new Text(suggestedWord + " " + secondWord); + Text highlighted = new Text("" + suggestedWord + " " + secondWord); + PhraseSuggestion.Entry.Option option1 = new Option(suggestionText, highlighted, 0.7f, false); + PhraseSuggestion.Entry.Option option2 = new Option(suggestionText, highlighted, 0.8f, true); + PhraseSuggestion.Entry.Option option3 = new Option(suggestionText, highlighted, 0.6f); + assertEquals(suggestionText, option1.getText()); + assertEquals(highlighted, option1.getHighlighted()); + assertFalse(option1.collateMatch()); + assertTrue(option1.getScore() > 0.6f); + option1.mergeInto(option2); + assertEquals(suggestionText, option1.getText()); + assertEquals(highlighted, option1.getHighlighted()); + assertTrue(option1.collateMatch()); + assertTrue(option1.getScore() > 0.7f); + option1.mergeInto(option3); + assertEquals(suggestionText, option1.getText()); + assertEquals(highlighted, option1.getHighlighted()); + assertTrue(option1.getScore() > 0.7f); + assertTrue(option1.collateMatch()); + } }