Skip to content

Commit

Permalink
Fixed "max_analyzer_offset" query parameter ignored by "plain" highli…
Browse files Browse the repository at this point in the history
…ghter.

Signed-off-by: Thomas Seidl <[email protected]>
  • Loading branch information
drunken-monkey committed Jan 28, 2024
1 parent 8eb91ee commit 12adef4
Showing 1 changed file with 15 additions and 1 deletion.
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 12adef4

Please sign in to comment.