Complete the code to specify the field to highlight in an Elasticsearch query.
{
"query": {
"match": {
"content": "search text"
}
},
"highlight": {
"fields": {
"[1]": {}
}
}
}The highlight section must specify the field name where matches are highlighted. Here, the field is content.
Complete the code to set the pre-tags for highlighted text in Elasticsearch.
{
"highlight": {
"pre_tags": ["[1]"],
"post_tags": ["</em>"],
"fields": {
"content": {}
}
}
}The pre_tags define the opening tag for highlighted text. Commonly, <em> is used to emphasize matched text.
Fix the error in the highlight section to correctly specify multiple fields to highlight.
{
"highlight": {
"fields": {
"title": {},
"[1]": {}
}
}
}fields or match as field names.To highlight multiple fields, list each field name under fields. Here, content is the second field to highlight.
Fill both blanks to highlight the field and set the fragment size in Elasticsearch.
{
"highlight": {
"fields": {
"[1]": {
"fragment_size": [2]
}
}
}
}The field to highlight is content. The fragment_size controls the size of the highlighted snippet, here set to 150 characters.
Fill all three blanks to highlight a field with custom tags and number of fragments.
{
"highlight": {
"pre_tags": ["[1]"],
"post_tags": ["[2]"],
"fields": {
"content": {
"number_of_fragments": [3]
}
}
}
}The pre_tags and post_tags define the HTML tags wrapping the highlighted text. The number_of_fragments controls how many highlighted snippets are returned, here 3.