-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Description
Allow to highlight search results on one or more fields. The implementation uses the lucene fast-vector-highlighter. The search request body:
{
query : {...},
highlight : {
fields : {
"_all" : {}
}
}
}
In the above case, the _all field will be highlighted for each search hit (there will be another element in each search each, called highlight, which includes the highlighted fields and the highlighted fragments).
Note, in order to highlight, the field in question must be stored and have termVector of with_positions_offsets. In the above case, the mapping should include:
{
typeName : {
allField : {store : "yes", termVector : "with_positions_offsets"}
}
}
The _all field can be an easy to use candidate for highlighting.
By default, the highlighting will wrap highlighted text in <em> and </em>. This can be controlled by setting preTags and postTags, for example:
{
query : {...},
highlight : {
preTags : ["<tag1>", "<tag2>"],
postTags : ["</tag1>", "</tag2>"]
fields : {
"_all" : {}
}
}
}
There can be a single tag or more, and the "importance" is ordered. There are also built in "tag" schemas, with currently a single schema called styled with preTags of:
<em class="hlt1">, <em class="hlt2">, <em class="hlt2">,
<em class="hlt3">, <em class="hlt4">, <em class="hlt5">,
<em class="hlt6">, <em class="hlt7">, <em class="hlt8">,
<em class="hlt9">
And post tag of </em>. If you think of more nice to have built in tag schemas, just commend on this issue or post another issue to create it. Here is an example of switching tag schemas:
{
query : {...},
highlight : {
tagSchema : "styled",
fields : {
"_all" : {}
}
}
}
Each field highlighted can control the size of the highlighted fragment in characters (defaults to 100), and the maximum number of fragments to return (defaults to 5). For example:
{
query : {...},
highlight : {
fields : {
"_all" : {fragmentSize : 150, numberOfFragments : 3}
}
}
}