Skip to content

Commit

Permalink
Fix the "highlight.max_analyzer_offset" request parameter with "plain…
Browse files Browse the repository at this point in the history
…" highlighter (opensearch-project#10919)

(cherry picked from commit 5d327a2)
  • Loading branch information
drunken-monkey committed Feb 17, 2024
1 parent 3b47fa3 commit 02b6504
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
- Prevent setting remote_snapshot store type on index creation ([#11867](https://github.com/opensearch-project/OpenSearch/pull/11867))
- [BUG] Fix remote shards balancer when filtering throttled nodes ([#11724](https://github.com/opensearch-project/OpenSearch/pull/11724))
- [Bug] Check phase name before SearchRequestOperationsListener onPhaseStart ([#12094](https://github.com/opensearch-project/OpenSearch/pull/12094))
- Fix the "highlight.max_analyzer_offset" request parameter with "plain" highlighter ([#10919](https://github.com/opensearch-project/OpenSearch/pull/10919))

### Security

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,15 @@ setup:
index: test1
body: {"query" : {"match" : {"field2" : "fox"}}, "highlight" : {"type" : "plain", "fields" : {"field2" : {}}}}
- match: { error.root_cause.0.type: "illegal_argument_exception" }

---
"Plain highlighter on a field WITHOUT OFFSETS using max_analyzer_offset should SUCCEED":
- skip:
version: " - 2.1.99"
reason: only starting supporting the parameter max_analyzer_offset on version 2.2
- do:
search:
rest_total_hits_as_int: true
index: test1
body: {"query" : {"match" : {"field1" : "quick"}}, "highlight" : {"type" : "plain", "fields" : {"field1" : {"max_analyzer_offset": 10}}}}
- match: {hits.hits.0.highlight.field1.0: "The <em>quick</em> "}
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,27 @@ public HighlightField highlight(FieldHighlightContext fieldContext) throws IOExc
List<Object> textsToHighlight;
Analyzer analyzer = context.mapperService().documentMapper().mappers().indexAnalyzer();
final int maxAnalyzedOffset = context.getIndexSettings().getHighlightMaxAnalyzedOffset();
final Integer fieldMaxAnalyzedOffset = field.fieldOptions().maxAnalyzerOffset();
if (fieldMaxAnalyzedOffset != null && fieldMaxAnalyzedOffset > maxAnalyzedOffset) {
throw new IllegalArgumentException(
"max_analyzer_offset has exceeded ["
+ maxAnalyzedOffset
+ "] - maximum allowed to be analyzed for highlighting. "
+ "This maximum can be set by changing the ["
+ IndexSettings.MAX_ANALYZED_OFFSET_SETTING.getKey()
+ "] index level setting. "
+ "For large texts, indexing with offsets or term vectors is recommended!"
);
}

textsToHighlight = HighlightUtils.loadFieldValues(fieldType, context.getQueryShardContext(), hitContext, fieldContext.forceSource);

for (Object textToHighlight : textsToHighlight) {
String text = convertFieldValue(fieldType, textToHighlight);
int textLength = text.length();
if (textLength > maxAnalyzedOffset) {
if (fieldMaxAnalyzedOffset != null && textLength > fieldMaxAnalyzedOffset) {
text = text.substring(0, fieldMaxAnalyzedOffset);
} else if (textLength > maxAnalyzedOffset) {
throw new IllegalArgumentException(
"The length of ["
+ fieldContext.fieldName
Expand Down

0 comments on commit 02b6504

Please sign in to comment.