Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/Nest/Search/Search/Highlighting/HighlighterType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,39 @@ public enum HighlighterType
{
/// <summary>
/// Plain Highlighter.
/// The default choice of highlighter is of type plain and uses the Lucene highlighter.
/// It tries hard to reflect the query matching logic in terms of understanding word
/// The default choice of highlighter is of type plain and uses the Lucene highlighter.
/// It tries hard to reflect the query matching logic in terms of understanding word
/// importance and any word positioning criteria in phrase queries.
/// </summary>
[EnumMember(Value = "plain")]
Plain,

/// <summary>
/// Postings Highlighter.
/// If index_options is set to offsets in the mapping the postings highlighter
/// If index_options is set to offsets in the mapping the postings highlighter
/// will be used instead of the plain highlighter
/// </summary>
[EnumMember(Value = "postings")]
Postings,

/// <summary>
/// Fast Vector Highlighter.
/// If term_vector information is provided by setting term_vector to with_positions_offsets
/// If term_vector information is provided by setting term_vector to with_positions_offsets
/// in the mapping then the fast vector highlighter will be used instead of the plain highlighter
/// </summary>
[EnumMember(Value = "fvh")]
Fvh
Fvh,


/// <summary>
/// Unified Highlighter.
/// The unified highlighter can extract offsets from either postings, term vectors, or via re-analyzing text.
/// Under the hood it uses Lucene UnifiedHighlighter which picks its strategy depending on the field and the query to highlight.
/// Independently of the strategy this highlighter breaks the text into sentences and scores individual sentences as if
/// they were documents in this corpus, using the BM25 algorithm. It supports accurate phrase and multi-term
/// (fuzzy, prefix, regex) highlighting
/// </summary>
[EnumMember(Value = "unified")]
Unified
}
}
53 changes: 53 additions & 0 deletions src/Tests/Search/Request/HighlightingUsageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ public class HighlightingUsageTests : SearchUsageTestBase
{
public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cluster, usage) { }

public string LastNameSearch { get; } = Project.Projects.First().LeadDeveloper.LastName;

protected override object ExpectJson => new
{
query = new
Expand Down Expand Up @@ -72,6 +74,26 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
}
}
},
{ "leadDeveloper.lastName", new JObject
{
{ "type", "unified" },
{ "pre_tags", new JArray { "<name>" } },
{ "post_tags", new JArray { "</name>" } },
{ "highlight_query", new JObject
{
{ "match", new JObject
{
{ "leadDeveloper.lastName", new JObject
{
{ "query", LastNameSearch }
}
}
}
}
}
}
}
},
{ "state.offsets", new JObject
{
{ "type", "postings" },
Expand Down Expand Up @@ -122,6 +144,17 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
.Query("Kurt Edgardo Naomi Dariana Justice Felton")
)
),
fs => fs
.Field(p => p.LeadDeveloper.LastName)
.Type(HighlighterType.Unified)
.PreTags("<name>")
.PostTags("</name>")
.HighlightQuery(q => q
.Match(m => m
.Field(p => p.LeadDeveloper.LastName)
.Query(LastNameSearch)
)
),
fs => fs
.Field(p => p.State.Suffix("offsets"))
.Type(HighlighterType.Postings)
Expand Down Expand Up @@ -175,6 +208,18 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
}
}
},
{ "leadDeveloper.lastName", new HighlightField
{
Type = HighlighterType.Unified,
PreTags = new[] { "<name>"},
PostTags = new[] { "</name>"},
HighlightQuery = new MatchQuery
{
Field = "leadDeveloper.lastName",
Query = LastNameSearch
}
}
},
{ "state.offsets", new HighlightField
{
Type = HighlighterType.Postings,
Expand Down Expand Up @@ -215,6 +260,14 @@ protected override void ExpectResponse(ISearchResponse<Project> response)
highlight.Should().Contain("</name>");
}
}
else if (highlightField.Key == "leadDeveloper.lastName ")
{
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)
Expand Down