Skip to content

Commit d4b10df

Browse files
committed
Add Params to PhraseSuggestCollate
PhraseSuggestCollate Query takes an ITemplateQuery that allows the specification of parameters. The API of phrase suggester however specifies the parameters for the query at the same level as the query, not nested within the query object. This commit adds a Parameters dictionary to IPhraseSuggestCollate so that parameters are serialized to the correct query form. If parameters are specified on the ITemplateQuery, these are assigned to the parameters on IPhraseSuggestCollate. Add XML documentation to indicate the usage of each property. Closes #2849
1 parent f640b05 commit d4b10df

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

src/Nest/Search/Suggesters/PhraseSuggester/PhraseSuggestCollate.cs

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,99 @@
44

55
namespace Nest
66
{
7+
/// <summary>
8+
/// Checks each suggestion against the specified query to prune suggestions
9+
/// for which no matching docs exist in the index.
10+
/// </summary>
711
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
812
[JsonConverter(typeof(ReadAsTypeJsonConverter<PhraseSuggestCollate>))]
913
public interface IPhraseSuggestCollate
1014
{
11-
[JsonProperty(PropertyName = "query")]
15+
/// <summary>
16+
/// The collate query to run.
17+
/// </summary>
18+
/// <remarks>
19+
/// Query parameters should be specified using <see cref="Params"/>
20+
/// </remarks>
21+
[JsonProperty("query")]
1222
ITemplateQuery Query { get; set; }
1323

14-
[JsonProperty(PropertyName = "prune")]
24+
/// <summary>
25+
/// Controls if all phrase suggestions will be returned. When set to <c>true</c>, the suggestions will have
26+
/// an additional option collate_match, which will be <c>true</c> if matching documents for the phrase was found,
27+
/// <c>false</c> otherwise. The default value for <see cref="Prune"/> is <c>false</c>.
28+
/// </summary>
29+
[JsonProperty("prune")]
1530
bool? Prune { get; set; }
31+
32+
/// <summary>
33+
/// The parameters for the query. the suggestion value will be added to the variables you specify.
34+
/// </summary>
35+
[JsonProperty("params")]
36+
IDictionary<string, object> Params { get; set; }
1637
}
1738

39+
/// <inheritdoc />
1840
public class PhraseSuggestCollate : IPhraseSuggestCollate
1941
{
20-
public ITemplateQuery Query { get; set; }
42+
private ITemplateQuery _query;
43+
44+
/// <inheritdoc />
45+
public ITemplateQuery Query
46+
{
47+
get => _query;
48+
set
49+
{
50+
_query = value;
51+
if (_query != null) Params = _query.Params;
52+
}
53+
}
2154

55+
/// <inheritdoc />
2256
public bool? Prune { get; set; }
57+
58+
/// <inheritdoc />
59+
public IDictionary<string, object> Params { get; set; }
2360
}
2461

2562
public class PhraseSuggestCollateDescriptor<T> : DescriptorBase<PhraseSuggestCollateDescriptor<T>, IPhraseSuggestCollate>, IPhraseSuggestCollate
2663
where T : class
2764
{
2865
ITemplateQuery IPhraseSuggestCollate.Query { get; set; }
29-
66+
IDictionary<string, object> IPhraseSuggestCollate.Params { get; set; }
3067
bool? IPhraseSuggestCollate.Prune { get; set; }
3168

69+
/// <summary>
70+
/// The collate query to run.
71+
/// </summary>
72+
/// <remarks>
73+
/// Query parameters should be specified using <see cref="Params(IDictionary&lt;string, object&gt;)"/> or
74+
/// Params(Func&lt;FluentDictionary&lt;string, object&gt;, FluentDictionary&lt;string, object&gt;&gt;)
75+
/// </remarks>
3276
public PhraseSuggestCollateDescriptor<T> Query(Func<TemplateQueryDescriptor<T>, ITemplateQuery> selector) =>
33-
Assign(a => a.Query = selector?.Invoke(new TemplateQueryDescriptor<T>()));
77+
Assign(a =>
78+
{
79+
var templateQuery = selector?.Invoke(new TemplateQueryDescriptor<T>());
80+
a.Query = templateQuery;
81+
if (templateQuery != null) Self.Params = templateQuery.Params;
82+
});
3483

84+
/// <summary>
85+
/// Controls if all phrase suggestions will be returned. When set to <c>true</c>, the suggestions will have
86+
/// an additional option collate_match, which will be <c>true</c> if matching documents for the phrase was found,
87+
/// <c>false</c> otherwise. The default value for <see cref="Prune"/> is <c>false</c>.
88+
/// </summary>
3589
public PhraseSuggestCollateDescriptor<T> Prune(bool? prune = true) => Assign(a => a.Prune = prune);
90+
91+
/// <summary>
92+
/// The parameters for the query. the suggestion value will be added to the variables you specify.
93+
/// </summary>
94+
public PhraseSuggestCollateDescriptor<T> Params(IDictionary<string, object> paramsDictionary) => Assign(a => a.Params = paramsDictionary);
95+
96+
/// <summary>
97+
/// The parameters for the query. the suggestion value will be added to the variables you specify.
98+
/// </summary>
99+
public PhraseSuggestCollateDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramsDictionary) =>
100+
Assign(a => a.Params = paramsDictionary(new FluentDictionary<string, object>()));
36101
}
37102
}

src/Tests/Search/Suggesters/SuggestApiTests.cs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ protected override LazyResponses ClientUsage() => Calls(
8080
.Confidence(1)
8181
.Collate(c => c
8282
.Query(q => q
83-
.Inline("{ \"match_all\": { }}")
84-
.Params(p => p.Add("field_name", _phraseSuggestField))
83+
.Inline("{ \"match\": { \"{{field_name}}\" : \"{{suggestion}}\" }}")
8584
)
85+
.Params(p => p.Add("field_name", _phraseSuggestField))
8686
.Prune()
8787
)
8888
.DirectGenerator(d => d
@@ -170,11 +170,11 @@ protected override LazyResponses ClientUsage() => Calls(
170170
{
171171
Query = new TemplateQuery
172172
{
173-
Inline = "{ \"match_all\": { }}",
174-
Params = new Dictionary<string, object>
175-
{
176-
{"field_name", _phraseSuggestField}
177-
}
173+
Inline = "{ \"match\": { \"{{field_name}}\" : \"{{suggestion}}\" }}",
174+
},
175+
Params = new Dictionary<string, object>
176+
{
177+
{"field_name", _phraseSuggestField}
178178
},
179179
Prune = true
180180
},
@@ -322,11 +322,11 @@ private static void AssertCompletionSuggestResponse(ISuggestResponse<Project> re
322322
{
323323
query = new
324324
{
325-
inline = "{ \"match_all\": { }}",
326-
@params = new
327-
{
328-
field_name = _phraseSuggestField
329-
}
325+
inline = "{ \"match\": { \"{{field_name}}\" : \"{{suggestion}}\" }}",
326+
},
327+
@params = new
328+
{
329+
field_name = _phraseSuggestField
330330
},
331331
prune = true,
332332
},

0 commit comments

Comments
 (0)