Skip to content

Commit 9fc0fcb

Browse files
Disallow 0
1 parent bf90d9a commit 9fc0fcb

File tree

4 files changed

+13
-9
lines changed

4 files changed

+13
-9
lines changed

docs/reference/search/search-your-data/highlighting.asciidoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -262,11 +262,11 @@ max_analyzed_offset:: By default, the maximum number of characters
262262
analyzed for a highlight request is bounded by the value defined in the
263263
<<index-max-analyzed-offset, `index.highlight.max_analyzed_offset`>> setting,
264264
and when the number of characters exceeds this limit an error is returned. If
265-
this setting is set to a non-negative (>=0) value, the highlighting stops at this defined
265+
this setting is set to a positive value, the highlighting stops at this defined
266266
maximum limit, and the rest of the text is not processed, thus not highlighted and
267-
no error is returned. If it is set to -1 then the value of
267+
no error is returned. If it is specifically set to -1 then the value of
268268
<<index-max-analyzed-offset, `index.highlight.max_analyzed_offset`>> is used instead.
269-
For values <= -1, an error is returned. The <<max-analyzed-offset, `max_analyzed_offset`>> query setting
269+
For values < -1 or 0, an error is returned. The <<max-analyzed-offset, `max_analyzed_offset`>> query setting
270270
does *not* override the <<index-max-analyzed-offset, `index.highlight.max_analyzed_offset`>>
271271
which prevails when it's set to lower value than the query setting.
272272

rest-api-spec/src/yamlRestTest/resources/rest-api-spec/test/search.highlight/30_max_analyzed_offset.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,12 @@ setup:
118118
"Plain highlighter on a field WITH OFFSETS exceeding index.highlight.max_analyzed_offset with max_analyzed_offset=0 should SUCCEED":
119119

120120
- requires:
121-
cluster_features: ["gte_v7.12.0"]
122-
reason: max_analyzed_offset query param added in 7.12.0
121+
test_runner_features: [capabilities]
122+
capabilities:
123+
- method: GET
124+
path: /_search
125+
capabilities: [ highlight_max_analyzed_offset_default ]
126+
reason: Behavior of max_analyzed_offset query param changed in 8.18.
123127

124128
- do:
125129
search:
@@ -180,4 +184,4 @@ setup:
180184
- match: { status: 400 }
181185
- match: { error.root_cause.0.type: "x_content_parse_exception" }
182186
- match: { error.caused_by.type: "illegal_argument_exception" }
183-
- match: { error.caused_by.reason: "[max_analyzed_offset] must be an integer >= -1" }
187+
- match: { error.caused_by.reason: "[max_analyzed_offset] must be a positive integer, or -1" }

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,8 @@ public Integer phraseLimit() {
565565
*/
566566
@SuppressWarnings("unchecked")
567567
public HB maxAnalyzedOffset(Integer maxAnalyzedOffset) {
568-
if (maxAnalyzedOffset != null && maxAnalyzedOffset < -1) {
569-
throw new IllegalArgumentException("[" + MAX_ANALYZED_OFFSET_FIELD + "] must be an integer >= -1");
568+
if (maxAnalyzedOffset != null && (maxAnalyzedOffset < -1 || maxAnalyzedOffset == 0)) {
569+
throw new IllegalArgumentException("[" + MAX_ANALYZED_OFFSET_FIELD + "] must be a positive integer, or -1");
570570
}
571571
this.maxAnalyzedOffset = maxAnalyzedOffset;
572572
return (HB) this;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ public void testInvalidMaxAnalyzedOffset() throws IOException {
580580
"{ \"max_analyzed_offset\" : " + randomIntBetween(-100, -1) + "}"
581581
);
582582
assertThat(e.getMessage(), containsString("[highlight] failed to parse field [" + MAX_ANALYZED_OFFSET_FIELD.toString() + "]"));
583-
assertThat(e.getCause().getMessage(), containsString("[max_analyzed_offset] must be an integer >= -1"));
583+
assertThat(e.getCause().getMessage(), containsString("[max_analyzed_offset] must be a positive integer, or -1"));
584584
}
585585

586586
/**

0 commit comments

Comments
 (0)