Skip to content

Commit 7698c8c

Browse files
Add global highlight_query (#4863) (#4867)
This commit adds a global highlight_query to highlighter options. Closes #4862 Co-authored-by: Russ Cam <russ.cam@elastic.co>
1 parent 7288c9b commit 7698c8c

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

docs/search/request/highlighting-usage.asciidoc

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,12 @@ s => s
3636
.PreTags("<tag1>")
3737
.PostTags("</tag1>")
3838
.Encoder(HighlighterEncoder.Html)
39+
.HighlightQuery(q => q
40+
.Match(m => m
41+
.Field(f => f.Name.Suffix("standard"))
42+
.Query("Upton Sons Shield Rice Rowe Roberts")
43+
)
44+
)
3945
.Fields(
4046
fs => fs
4147
.Field(p => p.Name.Suffix("standard"))
@@ -90,6 +96,11 @@ new SearchRequest<Project>
9096
PreTags = new[] { "<tag1>" },
9197
PostTags = new[] { "</tag1>" },
9298
Encoder = HighlighterEncoder.Html,
99+
HighlightQuery = new MatchQuery
100+
{
101+
Query = "Upton Sons Shield Rice Rowe Roberts",
102+
Field = "name.standard"
103+
},
93104
Fields = new Dictionary<Field, IHighlightField>
94105
{
95106
{
@@ -155,6 +166,13 @@ new SearchRequest<Project>
155166
"</tag1>"
156167
],
157168
"encoder": "html",
169+
"highlight_query": {
170+
"match": {
171+
"name.standard": {
172+
"query": "Upton Sons Shield Rice Rowe Roberts"
173+
}
174+
}
175+
},
158176
"fields": {
159177
"name.standard": {
160178
"type": "plain",

src/Nest/Search/Search/Highlighting/Highlight.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,12 @@ public interface IHighlight
7373
[DataMember(Name ="fragment_size")]
7474
int? FragmentSize { get; set; }
7575

76+
/// <summary>
77+
/// The query to use for highlighting
78+
/// </summary>
79+
[DataMember(Name = "highlight_query")]
80+
QueryContainer HighlightQuery { get; set; }
81+
7682
[DataMember(Name ="max_fragment_length")]
7783
int? MaxFragmentLength { get; set; }
7884

@@ -169,6 +175,9 @@ public class Highlight : IHighlight
169175
/// <inheritdoc/>
170176
public int? FragmentSize { get; set; }
171177

178+
/// <inheritdoc/>
179+
public QueryContainer HighlightQuery { get; set; }
180+
172181
/// <inheritdoc/>
173182
public int? MaxFragmentLength { get; set; }
174183

@@ -214,6 +223,7 @@ public class HighlightDescriptor<T> : DescriptorBase<HighlightDescriptor<T>, IHi
214223
HighlighterFragmenter? IHighlight.Fragmenter { get; set; }
215224
int? IHighlight.FragmentOffset { get; set; }
216225
int? IHighlight.FragmentSize { get; set; }
226+
QueryContainer IHighlight.HighlightQuery { get; set; }
217227
int? IHighlight.MaxFragmentLength { get; set; }
218228
int? IHighlight.NoMatchSize { get; set; }
219229
int? IHighlight.NumberOfFragments { get; set; }
@@ -253,6 +263,10 @@ public HighlightDescriptor<T> Fields(params Func<HighlightFieldDescriptor<T>, IH
253263
/// <inheritdoc cref="IHighlight.FragmentSize" />
254264
public HighlightDescriptor<T> FragmentSize(int? fragmentSize) => Assign(fragmentSize, (a, v) => a.FragmentSize = v);
255265

266+
/// <inheritdoc cref="IHighlight.HighlightQuery" />
267+
public HighlightDescriptor<T> HighlightQuery(Func<QueryContainerDescriptor<T>, QueryContainer> query) =>
268+
Assign(query, (a, v) => a.HighlightQuery = v?.Invoke(new QueryContainerDescriptor<T>()));
269+
256270
/// <inheritdoc cref="IHighlight.NumberOfFragments" />
257271
public HighlightDescriptor<T> NumberOfFragments(int? numberOfFragments) => Assign(numberOfFragments, (a, v) => a.NumberOfFragments = v);
258272

tests/Tests/Search/Request/HighlightingUsageTests.cs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,18 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
4646
pre_tags = new[] { "<tag1>" },
4747
post_tags = new[] { "</tag1>" },
4848
encoder = "html",
49+
highlight_query = new
50+
{
51+
match = new Dictionary<string, object>
52+
{
53+
{
54+
"name.standard", new Dictionary<string, object>
55+
{
56+
{ "query", "Upton Sons Shield Rice Rowe Roberts" }
57+
}
58+
}
59+
}
60+
},
4961
fields = new Dictionary<string, object>
5062
{
5163
{
@@ -124,6 +136,12 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
124136
.PreTags("<tag1>")
125137
.PostTags("</tag1>")
126138
.Encoder(HighlighterEncoder.Html)
139+
.HighlightQuery(q => q
140+
.Match(m => m
141+
.Field(f => f.Name.Suffix("standard"))
142+
.Query("Upton Sons Shield Rice Rowe Roberts")
143+
)
144+
)
127145
.Fields(
128146
fs => fs
129147
.Field(p => p.Name.Suffix("standard"))
@@ -173,6 +191,11 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
173191
PreTags = new[] { "<tag1>" },
174192
PostTags = new[] { "</tag1>" },
175193
Encoder = HighlighterEncoder.Html,
194+
HighlightQuery = new MatchQuery
195+
{
196+
Query = "Upton Sons Shield Rice Rowe Roberts",
197+
Field = "name.standard"
198+
},
176199
Fields = new Dictionary<Field, IHighlightField>
177200
{
178201
{

0 commit comments

Comments
 (0)