Skip to content

Commit f26f2c7

Browse files
Add test
1 parent 2faea54 commit f26f2c7

File tree

2 files changed

+37
-5
lines changed

2 files changed

+37
-5
lines changed

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

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2632,6 +2632,42 @@ public void testPostingsHighlighterOrderByScore() throws Exception {
26322632
});
26332633
}
26342634

2635+
public void testMaxQueryOffsetDefault() throws Exception {
2636+
assertAcked(
2637+
prepareCreate("test").setMapping(type1PostingsffsetsMapping())
2638+
.setSettings(Settings.builder().put("index.highlight.max_analyzed_offset", "10").build())
2639+
);
2640+
ensureGreen();
2641+
2642+
prepareIndex("test").setSource(
2643+
"field1",
2644+
new String[] {
2645+
"This sentence contains one match, not that short. This sentence contains zero sentence matches. "
2646+
+ "This one contains no matches.",
2647+
"This is the second value's first sentence. This one contains no matches. "
2648+
+ "This sentence contains three sentence occurrences (sentence).",
2649+
"One sentence match here and scored lower since the text is quite long, not that appealing. "
2650+
+ "This one contains no matches." }
2651+
).get();
2652+
refresh();
2653+
2654+
logger.info("--> highlighting and searching on field1");
2655+
// Specific for this test: by passing "-1" as "maxAnalyzedOffset", the index highlight setting above will be used.
2656+
SearchSourceBuilder source = searchSource().query(termQuery("field1", "sentence"))
2657+
.highlighter(highlight().field("field1").order("score").maxAnalyzedOffset(-1));
2658+
2659+
assertResponse(client().search(new SearchRequest("test").source(source)), response -> {
2660+
Map<String, HighlightField> highlightFieldMap = response.getHits().getAt(0).getHighlightFields();
2661+
assertThat(highlightFieldMap.size(), equalTo(1));
2662+
HighlightField field1 = highlightFieldMap.get("field1");
2663+
assertThat(field1.fragments().length, equalTo(1));
2664+
assertThat(
2665+
field1.fragments()[0].string(),
2666+
equalTo("This <em>sentence</em> contains one match, not that short. This sentence contains zero sentence matches.")
2667+
);
2668+
});
2669+
}
2670+
26352671
public void testPostingsHighlighterEscapeHtml() throws Exception {
26362672
assertAcked(prepareCreate("test").setMapping("title", "type=text," + randomStoreField() + "index_options=offsets"));
26372673

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -561,14 +561,10 @@ public Integer phraseLimit() {
561561
}
562562

563563
/**
564-
* Set to a non-negative value which represents the max offset used to analyze
565-
* the field thus avoiding exceptions if the field exceeds this limit.
564+
* "maxAnalyzedOffset" might be non-negative int, null (unknown), or a negative int (defaulting to index analyzed offset).
566565
*/
567566
@SuppressWarnings("unchecked")
568567
public HB maxAnalyzedOffset(Integer maxAnalyzedOffset) {
569-
if (maxAnalyzedOffset != null && maxAnalyzedOffset <= 0) {
570-
throw new IllegalArgumentException("[" + MAX_ANALYZED_OFFSET_FIELD + "] must be a positive integer");
571-
}
572568
this.maxAnalyzedOffset = maxAnalyzedOffset;
573569
return (HB) this;
574570
}

0 commit comments

Comments
 (0)