You are looking at preliminary documentation for a future release.
Not what you want? See the
current release documentation.
Highlighting Usageedit
Allows to highlight search results on one or more fields.
The implementation uses either the lucene highlighter, fast-vector-highlighter or postings-highlighter.
See the Elasticsearch documentation on highlighting for more detail.
Fluent DSL Exampleedit
s => s
.Query(q => q
.Match(m => m
.Field(f => f.Name.Suffix("standard"))
.Query("Upton Sons Shield Rice Rowe Roberts")
)
)
.Highlight(h => h
.PreTags("<tag1>")
.PostTags("</tag1>")
.Fields(
fs => fs
.Field(p => p.Name.Suffix("standard"))
.Type("plain")
.ForceSource()
.FragmentSize(150)
.NumberOfFragments(3)
.NoMatchSize(150),
fs => fs
.Field(p => p.LeadDeveloper.FirstName)
.Type(HighlighterType.Fvh)
.PreTags("<name>")
.PostTags("</name>")
.BoundaryMaxScan(50)
.HighlightQuery(q => q
.Match(m => m
.Field(p => p.LeadDeveloper.FirstName)
.Query("Kurt Edgardo Naomi Dariana Justice Felton")
)
),
fs => fs
.Field(p => p.State.Suffix("offsets"))
.Type(HighlighterType.Postings)
.PreTags("<state>")
.PostTags("</state>")
.HighlightQuery(q => q
.Terms(t => t
.Field(f => f.State.Suffix("offsets"))
.Terms(
StateOfBeing.Stable.ToString().ToLowerInvariant(),
StateOfBeing.BellyUp.ToString().ToLowerInvariant()
)
)
)
)
)Object Initializer Syntax Exampleedit
new SearchRequest<Project>
{
Query = new MatchQuery
{
Query = "Upton Sons Shield Rice Rowe Roberts",
Field = "name.standard"
},
Highlight = new Highlight
{
PreTags = new[] { "<tag1>" },
PostTags = new[] { "</tag1>" },
Fields = new Dictionary<Field, IHighlightField>
{
{ "name.standard", new HighlightField
{
Type = HighlighterType.Plain,
ForceSource = true,
FragmentSize = 150,
NumberOfFragments = 3,
NoMatchSize = 150
}
},
{ "leadDeveloper.firstName", new HighlightField
{
Type = "fvh",
BoundaryMaxScan = 50,
PreTags = new[] { "<name>"},
PostTags = new[] { "</name>"},
HighlightQuery = new MatchQuery
{
Field = "leadDeveloper.firstName",
Query = "Kurt Edgardo Naomi Dariana Justice Felton"
}
}
},
{ "state.offsets", new HighlightField
{
Type = HighlighterType.Postings,
PreTags = new[] { "<state>"},
PostTags = new[] { "</state>"},
HighlightQuery = new TermsQuery
{
Field = "state.offsets",
Terms = new [] { "stable", "bellyup" }
}
}
}
}
}
}Example json output.
{
"query": {
"match": {
"name.standard": {
"query": "Upton Sons Shield Rice Rowe Roberts"
}
}
},
"highlight": {
"pre_tags": [
"<tag1>"
],
"post_tags": [
"</tag1>"
],
"fields": {
"name.standard": {
"type": "plain",
"force_source": true,
"fragment_size": 150,
"number_of_fragments": 3,
"no_match_size": 150
},
"leadDeveloper.firstName": {
"type": "fvh",
"boundary_max_scan": 50,
"pre_tags": [
"<name>"
],
"post_tags": [
"</name>"
],
"highlight_query": {
"match": {
"leadDeveloper.firstName": {
"query": "Kurt Edgardo Naomi Dariana Justice Felton"
}
}
}
},
"state.offsets": {
"type": "postings",
"pre_tags": [
"<state>"
],
"post_tags": [
"</state>"
],
"highlight_query": {
"terms": {
"state.offsets": [
"stable",
"bellyup"
]
}
}
}
}
}
}
Handling Responsesedit
response.ShouldBeValid();
foreach (var highlightsInEachHit in response.Hits.Select(d=>d.Highlights))
{
foreach (var highlightField in highlightsInEachHit)
{
if (highlightField.Key == "name.standard")
{
foreach (var highlight in highlightField.Value.Highlights)
{
highlight.Should().Contain("<tag1>");
highlight.Should().Contain("</tag1>");
}
}
else if (highlightField.Key == "leadDeveloper.firstName")
{
foreach (var highlight in highlightField.Value.Highlights)
{
highlight.Should().Contain("<name>");
highlight.Should().Contain("</name>");
}
}
else if (highlightField.Key == "state.offsets")
{
foreach (var highlight in highlightField.Value.Highlights)
{
highlight.Should().Contain("<state>");
highlight.Should().Contain("</state>");
}
}
else
{
Assert.True(false, $"highlights contains unexpected key {highlightField.Key}");
}
}
}