Skip to content

Commit 043c1f5

Browse files
committed
Unified highlighter should respect no_match_size with number_of_fragments set to 0 (#41069)
The unified highlighter returns the first sentence of the text when number_of_fragments is set to 0 (full highlighting). This is a legacy of the removed postings highlighter that was based on sentence break only. This commit changes this behavior in order to respect the provided no_match_size value when number_of_fragments is set to 0. This means that the behavior will be consistent for any value of the number_of_fragments option. Closes #41066
1 parent 9bf8bd4 commit 043c1f5

File tree

2 files changed

+5
-40
lines changed

2 files changed

+5
-40
lines changed

server/src/main/java/org/elasticsearch/search/fetch/subphase/highlight/UnifiedHighlighter.java

Lines changed: 0 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ public HighlightField highlight(HighlighterContext highlighterContext) {
124124
"Failed to highlight field [" + highlighterContext.fieldName + "]", e);
125125
}
126126

127-
snippets = filterSnippets(snippets, field.fieldOptions().numberOfFragments());
128-
129127
if (field.fieldOptions().scoreOrdered()) {
130128
//let's sort the snippets by score if needed
131129
CollectionUtil.introSort(snippets, (o1, o2) -> Double.compare(o2.getScore(), o1.getScore()));
@@ -185,41 +183,6 @@ protected BreakIterator getBreakIterator(SearchContextHighlight.Field field) {
185183
}
186184
}
187185

188-
protected static List<Snippet> filterSnippets(List<Snippet> snippets, int numberOfFragments) {
189-
190-
//We need to filter the snippets as due to no_match_size we could have
191-
//either highlighted snippets or non highlighted ones and we don't want to mix those up
192-
List<Snippet> filteredSnippets = new ArrayList<>(snippets.size());
193-
for (Snippet snippet : snippets) {
194-
if (snippet.isHighlighted()) {
195-
filteredSnippets.add(snippet);
196-
}
197-
}
198-
199-
//if there's at least one highlighted snippet, we return all the highlighted ones
200-
//otherwise we return the first non highlighted one if available
201-
if (filteredSnippets.size() == 0) {
202-
if (snippets.size() > 0) {
203-
Snippet snippet = snippets.get(0);
204-
//if we tried highlighting the whole content using whole break iterator (as number_of_fragments was 0)
205-
//we need to return the first sentence of the content rather than the whole content
206-
if (numberOfFragments == 0) {
207-
BreakIterator bi = BreakIterator.getSentenceInstance(Locale.ROOT);
208-
String text = snippet.getText();
209-
bi.setText(text);
210-
int next = bi.next();
211-
if (next != BreakIterator.DONE) {
212-
String newText = text.substring(0, next).trim();
213-
snippet = new Snippet(newText, snippet.getScore(), snippet.isHighlighted());
214-
}
215-
}
216-
filteredSnippets.add(snippet);
217-
}
218-
}
219-
220-
return filteredSnippets;
221-
}
222-
223186
protected static String convertFieldValue(MappedFieldType type, Object value) {
224187
if (value instanceof BytesRef) {
225188
return type.valueForDisplay(value).toString();

server/src/test/java/org/elasticsearch/search/fetch/subphase/highlight/HighlighterSearchIT.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1715,9 +1715,11 @@ public void testHighlightNoMatchSize() throws IOException {
17151715
assertHighlight(response, 0, "text", 0, 1, equalTo("I am pretty long so some"));
17161716

17171717
// We can also ask for a fragment longer than the input string and get the whole string
1718-
field.highlighterType("plain").noMatchSize(text.length() * 2);
1719-
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
1720-
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
1718+
for (String type : new String[] { "plain", "unified" }) {
1719+
field.highlighterType(type).noMatchSize(text.length() * 2).numOfFragments(0);
1720+
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();
1721+
assertHighlight(response, 0, "text", 0, 1, equalTo(text));
1722+
}
17211723

17221724
field.highlighterType("fvh");
17231725
response = client().prepareSearch("test").highlighter(new HighlightBuilder().field(field)).get();

0 commit comments

Comments
 (0)