-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Fix highlighting on a stored keyword field #21645
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,6 +31,7 @@ | |
| import org.apache.lucene.search.highlight.SimpleHTMLFormatter; | ||
| import org.apache.lucene.search.highlight.SimpleSpanFragmenter; | ||
| import org.apache.lucene.search.highlight.TextFragment; | ||
| import org.apache.lucene.util.BytesRef; | ||
| import org.apache.lucene.util.BytesRefHash; | ||
| import org.apache.lucene.util.CollectionUtil; | ||
| import org.elasticsearch.ExceptionsHelper; | ||
|
|
@@ -106,7 +107,12 @@ public HighlightField highlight(HighlighterContext highlighterContext) { | |
| textsToHighlight = HighlightUtils.loadFieldValues(field, mapper, context, hitContext); | ||
|
|
||
| for (Object textToHighlight : textsToHighlight) { | ||
| String text = textToHighlight.toString(); | ||
| String text; | ||
| if (textToHighlight instanceof BytesRef) { | ||
| text = mapper.fieldType().valueForDisplay(textToHighlight).toString(); | ||
| } else { | ||
| text = textToHighlight.toString(); | ||
| } | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we just do
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The type of textToHighlight can be different whether it comes from the _source or from the stored fields. Calling valueForDisplay on it when it comes from _source would not work. |
||
|
|
||
| try (TokenStream tokenStream = analyzer.tokenStream(mapper.fieldType().name(), text)) { | ||
| if (!tokenStream.hasAttribute(CharTermAttribute.class) || !tokenStream.hasAttribute(OffsetAttribute.class)) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
would it work if we called MappedFieldType.valueForDisplay instead? I'm concerned some fields are stored as bytesrefs too but do not represent utf8 strings, like ip addresses
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that it's better to call valueForDisplay (I'll change the PR) though we are protected here since the highlighting can be done only on text or keyword fields.