Skip to content

Commit 5632144

Browse files
committed
Add phrase_limit to highlighting
- Add Encoder and PhraseLimit to integration test - Deprecate Encoder on HighlightField Closes #2851 (cherry picked from commit 56d9166)
1 parent 717281a commit 5632144

File tree

2 files changed

+62
-35
lines changed

2 files changed

+62
-35
lines changed

src/Nest/Search/Search/Highlighting/HighlightField.cs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ public interface IHighlightField
7575
/// Define how highlighted text will be encoded.
7676
/// It can be either default (no encoding) or html (will escape html, if you use html highlighting tags).
7777
/// </summary>
78-
[JsonProperty("encoder")]
78+
[JsonIgnore]
79+
[Obsolete("Encoder not valid on highlight field. Removed in NEST 6.x")]
7980
string Encoder { get; set; }
8081

8182
/// <summary>
@@ -133,6 +134,7 @@ public interface IHighlightField
133134
/// </summary>
134135
[JsonProperty("fragmenter")]
135136
HighlighterFragmenter? Fragmenter { get; set; }
137+
136138
/// <summary>
137139
/// The type of highlighter to use. Can be a defined or custom highlighter
138140
/// </summary>
@@ -159,6 +161,15 @@ public interface IHighlightField
159161
/// </summary>
160162
[JsonProperty("highlight_query")]
161163
QueryContainer HighlightQuery { get; set; }
164+
165+
/// <summary>
166+
/// Controls the number of matching phrases in a document that are considered. Prevents the
167+
/// <see cref="HighlighterType.Fvh"/> highlighter from analyzing too many phrases and consuming too much memory.
168+
/// When using matched_fields, <see cref="PhraseLimit"/> phrases per matched field are considered. Raising the limit increases query time
169+
/// and consumes more memory. Only supported by the <see cref="HighlighterType.Fvh"/> highlighter. Defaults to 256.
170+
/// </summary>
171+
[JsonProperty("phrase_limit")]
172+
int? PhraseLimit { get; set; }
162173
}
163174

164175
public class HighlightField : IHighlightField
@@ -180,6 +191,7 @@ public class HighlightField : IHighlightField
180191
/// <inheritdoc/>
181192
public int? BoundaryMaxScan { get; set; }
182193
/// <inheritdoc/>
194+
[Obsolete("Encoder not valid on highlight field. Removed in NEST 6.x")]
183195
public string Encoder { get; set; }
184196
/// <inheritdoc/>
185197
public string Order { get; set; }
@@ -205,6 +217,8 @@ public class HighlightField : IHighlightField
205217
public Fields MatchedFields { get; set; }
206218
/// <inheritdoc/>
207219
public QueryContainer HighlightQuery { get; set; }
220+
/// <inheritdoc/>
221+
public int? PhraseLimit { get; set; }
208222
}
209223

210224
public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescriptor<T>,IHighlightField>, IHighlightField
@@ -218,6 +232,7 @@ public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescript
218232
int? IHighlightField.NumberOfFragments { get; set; }
219233
int? IHighlightField.FragmentOffset { get; set; }
220234
int? IHighlightField.BoundaryMaxScan { get; set; }
235+
[Obsolete("Encoder not valid on highlight field. Removed in NEST 6.x")]
221236
string IHighlightField.Encoder { get; set; }
222237
string IHighlightField.Order { get; set; }
223238
string IHighlightField.TagsSchema { get; set; }
@@ -231,6 +246,7 @@ public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescript
231246
bool? IHighlightField.ForceSource { get; set; }
232247
Fields IHighlightField.MatchedFields { get; set; }
233248
QueryContainer IHighlightField.HighlightQuery { get; set; }
249+
int? IHighlightField.PhraseLimit { get; set; }
234250

235251
/// <inheritdoc/>
236252
public HighlightFieldDescriptor<T> Field(Field field) => Assign(a => a.Field = field);
@@ -278,6 +294,7 @@ public class HighlightFieldDescriptor<T> : DescriptorBase<HighlightFieldDescript
278294
public HighlightFieldDescriptor<T> FragmentOffset(int? fragmentOffset) => Assign(a => a.FragmentOffset = fragmentOffset);
279295

280296
/// <inheritdoc/>
297+
[Obsolete("Encoder not valid on highlight field. Removed in NEST 6.x")]
281298
public HighlightFieldDescriptor<T> Encoder(string encoder) => Assign(a => a.Encoder = encoder);
282299

283300
/// <inheritdoc/>
@@ -311,5 +328,8 @@ public HighlightFieldDescriptor<T> HighlightQuery(Func<QueryContainerDescriptor<
311328

312329
/// <inheritdoc/>
313330
public HighlightFieldDescriptor<T> Fragmenter(HighlighterFragmenter? fragmenter) => Assign(a => a.Fragmenter = fragmenter);
331+
332+
/// <inheritdoc/>
333+
public HighlightFieldDescriptor<T> PhraseLimit(int phraseLimit) => Assign(a => a.PhraseLimit = phraseLimit);
314334
}
315335
}

src/Tests/Search/Request/HighlightingUsageTests.cs

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
2929
{
3030
query = new
3131
{
32-
match = new JObject
32+
match = new Dictionary<string, object>
3333
{
34-
{ "name.standard", new JObject
34+
{ "name.standard", new Dictionary<string, object>
3535
{
3636
{ "query", "Upton Sons Shield Rice Rowe Roberts" }
3737
}
@@ -42,9 +42,10 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
4242
{
4343
pre_tags = new[] { "<tag1>" },
4444
post_tags = new[] { "</tag1>" },
45-
fields = new JObject
45+
encoder = "html",
46+
fields = new Dictionary<string, object>
4647
{
47-
{ "name.standard", new JObject
48+
{ "name.standard", new Dictionary<string, object>
4849
{
4950
{ "type", "plain" },
5051
{ "force_source", true},
@@ -54,17 +55,18 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
5455
{ "no_match_size", 150 }
5556
}
5657
},
57-
{ "leadDeveloper.firstName", new JObject
58+
{ "leadDeveloper.firstName", new Dictionary<string, object>
5859
{
5960
{ "type", "fvh" },
61+
{ "phrase_limit", 10 },
6062
{ "boundary_max_scan", 50 },
61-
{ "pre_tags", new JArray { "<name>" } },
62-
{ "post_tags", new JArray { "</name>" } },
63-
{ "highlight_query", new JObject
63+
{ "pre_tags", new [] { "<name>" } },
64+
{ "post_tags", new [] { "</name>" } },
65+
{ "highlight_query", new Dictionary<string, object>
6466
{
65-
{ "match", new JObject
67+
{ "match", new Dictionary<string, object>
6668
{
67-
{ "leadDeveloper.firstName", new JObject
69+
{ "leadDeveloper.firstName", new Dictionary<string, object>
6870
{
6971
{ "query", "Kurt Edgardo Naomi Dariana Justice Felton" }
7072
}
@@ -75,16 +77,20 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
7577
}
7678
}
7779
},
78-
{ "state.offsets", new JObject
80+
{ "leadDeveloper.lastName", new Dictionary<string, object>
7981
{
80-
{ "type", "postings" },
81-
{ "pre_tags", new JArray { "<state>" } },
82-
{ "post_tags", new JArray { "</state>" } },
83-
{ "highlight_query", new JObject
82+
{ "type", "unified" },
83+
{ "pre_tags", new [] { "<name>" } },
84+
{ "post_tags", new [] { "</name>" } },
85+
{ "highlight_query", new Dictionary<string, object>
8486
{
85-
{ "terms", new JObject
87+
{ "match", new Dictionary<string, object>
8688
{
87-
{ "state.offsets", new JArray { "stable" , "bellyup" } }
89+
{ "leadDeveloper.lastName", new Dictionary<string, object>
90+
{
91+
{ "query", LastNameSearch }
92+
}
93+
}
8894
}
8995
}
9096
}
@@ -105,6 +111,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
105111
.Highlight(h => h
106112
.PreTags("<tag1>")
107113
.PostTags("</tag1>")
114+
.Encoder("html")
108115
.Fields(
109116
fs => fs
110117
.Field(p => p.Name.Suffix("standard"))
@@ -120,24 +127,22 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
120127
.PreTags("<name>")
121128
.PostTags("</name>")
122129
.BoundaryMaxScan(50)
130+
.PhraseLimit(10)
123131
.HighlightQuery(q => q
124132
.Match(m => m
125133
.Field(p => p.LeadDeveloper.FirstName)
126134
.Query("Kurt Edgardo Naomi Dariana Justice Felton")
127135
)
128136
),
129137
fs => fs
130-
.Field(p => p.State.Suffix("offsets"))
131-
.Type(HighlighterType.Postings)
132-
.PreTags("<state>")
133-
.PostTags("</state>")
138+
.Field(p => p.LeadDeveloper.LastName)
139+
.Type(HighlighterType.Unified)
140+
.PreTags("<name>")
141+
.PostTags("</name>")
134142
.HighlightQuery(q => q
135-
.Terms(t => t
136-
.Field(f => f.State.Suffix("offsets"))
137-
.Terms(
138-
StateOfBeing.Stable.ToString().ToLowerInvariant(),
139-
StateOfBeing.BellyUp.ToString().ToLowerInvariant()
140-
)
143+
.Match(m => m
144+
.Field(p => p.LeadDeveloper.LastName)
145+
.Query(LastNameSearch)
141146
)
142147
)
143148
)
@@ -155,6 +160,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
155160
{
156161
PreTags = new[] { "<tag1>" },
157162
PostTags = new[] { "</tag1>" },
163+
Encoder = "html",
158164
Fields = new Dictionary<Field, IHighlightField>
159165
{
160166
{ "name.standard", new HighlightField
@@ -170,6 +176,7 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
170176
{ "leadDeveloper.firstName", new HighlightField
171177
{
172178
Type = "fvh",
179+
PhraseLimit = 10,
173180
BoundaryMaxScan = 50,
174181
PreTags = new[] { "<name>"},
175182
PostTags = new[] { "</name>"},
@@ -180,15 +187,15 @@ public HighlightingUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : ba
180187
}
181188
}
182189
},
183-
{ "state.offsets", new HighlightField
190+
{ "leadDeveloper.lastName", new HighlightField
184191
{
185-
Type = HighlighterType.Postings,
186-
PreTags = new[] { "<state>"},
187-
PostTags = new[] { "</state>"},
188-
HighlightQuery = new TermsQuery
192+
Type = HighlighterType.Unified,
193+
PreTags = new[] { "<name>"},
194+
PostTags = new[] { "</name>"},
195+
HighlightQuery = new MatchQuery
189196
{
190-
Field = "state.offsets",
191-
Terms = new [] { "stable", "bellyup" }
197+
Field = "leadDeveloper.lastName",
198+
Query = LastNameSearch
192199
}
193200
}
194201
}

0 commit comments

Comments
 (0)