Skip to content

Commit

Permalink
Check preTags and postTags params for empty values (elastic#106396)
Browse files Browse the repository at this point in the history
Check highlighter params of pre_tags and post_tags params for empty values.

If empty throw IllegalArgumentException.

Closes elastic#69009
  • Loading branch information
white-cc authored Mar 21, 2024
1 parent 3f8cc1e commit e5b6040
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 2 deletions.
6 changes: 6 additions & 0 deletions docs/changelog/106396.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pr: 106396
summary: "Check preTags and postTags params for empty values"
area: Highlighting
type: bug
issues:
- 69009
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
setup:
- skip:
version: ' - 8.13.99'
reason: 'check of preTags and postTags params for empty values was added in 8.14'

- do:
indices.create:
index: test
body:
mappings:
"properties":
"text":
"type": "text"
"term_vector": "with_positions_offsets"

- do:
index:
index: test
id: "1"
body:
"text" : "The quick brown fox is brown."
- do:
indices.refresh: {}

---
"Test with empty pre_tags or post_tags in query body with unified highlight type - should fail" :
- do:
catch: /pre_tags or post_tags must not be empty/
search:
index: test
body: {
"query": { "match": { "fox" } },
"highlight": {
"type": "unified",
"fields": { "*": { } },
"pre_tags": [ ],
"post_tags": [ ]
},
}

---
"Test with empty pre_tags or post_tags in query body with plain highlight type - should fail" :
- do:
catch: /pre_tags or post_tags must not be empty/
search:
index: test
body: {
"query": { "match": { "fox" } },
"highlight": {
"type": "plain",
"fields": { "*": { } },
"pre_tags": [ ],
"post_tags": [ ]
},
}

---
"Test with empty pre_tags or post_tags in query body with fvh highlight type - should fail" :
- do:
catch: /pre_tags or post_tags must not be empty/
search:
index: test
body: {
"query": { "match": { "fox" } },
"highlight": {
"type": "fvh",
"fields": { "*": { } },
"pre_tags": [ ],
"post_tags": [ ]
},
}

Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,9 @@ static <HB extends AbstractHighlighterBuilder<HB>> BiFunction<XContentParser, HB
if (hb.preTags() != null && hb.postTags() == null) {
throw new ParsingException(p.getTokenLocation(), "pre_tags are set but post_tags are not set");
}
if (hb.preTags() != null && hb.postTags() != null && (hb.preTags().length == 0 || hb.postTags().length == 0)) {
throw new ParsingException(p.getTokenLocation(), "pre_tags or post_tags must not be empty");
}
} catch (IOException e) {
throw new RuntimeException(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -610,8 +610,8 @@ public static HighlightBuilder randomHighlighterBuilder() {
private static void setRandomCommonOptions(AbstractHighlighterBuilder highlightBuilder) {
if (randomBoolean()) {
// need to set this together, otherwise parsing will complain
highlightBuilder.preTags(randomStringArray(0, 3));
highlightBuilder.postTags(randomStringArray(0, 3));
highlightBuilder.preTags(randomStringArray(1, 3));
highlightBuilder.postTags(randomStringArray(1, 3));
}
if (randomBoolean()) {
highlightBuilder.fragmentSize(randomIntBetween(0, 100));
Expand Down

0 comments on commit e5b6040

Please sign in to comment.