Skip to content

Commit a44a9bc

Browse files
committed
Add Params to PhraseSuggestCollate (#2965)
PhraseSuggestCollate Query takes an IScript 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 IScript, these are assigned to the parameters on IPhraseSuggestCollate. Add XML documentation to indicate the usage of each property. Closes #2849 (cherry picked from commit f905270)
1 parent b28f78b commit a44a9bc

File tree

4 files changed

+93
-34
lines changed

4 files changed

+93
-34
lines changed

docs/search/request/suggest-usage.asciidoc

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ s => s
6464
.Collate(c => c
6565
.Query(q => q
6666
.Inline("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
67-
.Params(p => p.Add("field_name", "title"))
6867
)
68+
.Params(p => p.Add("field_name", "title"))
6969
.Prune()
7070
)
7171
.Confidence(10.1)
@@ -142,12 +142,10 @@ new SearchRequest<Project>
142142
{
143143
Collate = new PhraseSuggestCollate
144144
{
145-
Query = new InlineScript("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
145+
Query = new InlineScript("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}"),
146+
Params = new Dictionary<string, object>
146147
{
147-
Params = new Dictionary<string, object>
148-
{
149-
{ "field_name", "title" }
150-
}
148+
{ "field_name", "title" }
151149
},
152150
Prune = true
153151
},
@@ -194,10 +192,10 @@ new SearchRequest<Project>
194192
"phrase": {
195193
"collate": {
196194
"query": {
197-
"inline": "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
198-
"params": {
199-
"field_name": "title"
200-
}
195+
"inline": "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}"
196+
},
197+
"params": {
198+
"field_name": "title"
201199
},
202200
"prune": true
203201
},

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

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,102 @@
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
IScript 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 IScript Query { get; set; }
42+
private IScript _query;
2143

44+
/// <inheritdoc />
45+
public IScript Query
46+
{
47+
get => _query;
48+
set
49+
{
50+
_query = value;
51+
if (_query != null) Params = _query.Params;
52+
}
53+
}
2254

55+
/// <inheritdoc />
2356
public bool? Prune { get; set; }
57+
58+
/// <inheritdoc />
59+
public IDictionary<string, object> Params { get; set; }
2460
}
2561

2662
public class PhraseSuggestCollateDescriptor<T> : DescriptorBase<PhraseSuggestCollateDescriptor<T>, IPhraseSuggestCollate>, IPhraseSuggestCollate
2763
where T : class
2864
{
2965
IScript IPhraseSuggestCollate.Query { get; set; }
30-
66+
IDictionary<string, object> IPhraseSuggestCollate.Params { get; set; }
3167
bool? IPhraseSuggestCollate.Prune { get; set; }
32-
68+
/// <summary>
69+
/// The collate query to run.
70+
/// </summary>
71+
/// <remarks>
72+
/// Query parameters should be specified using <see cref="Params(IDictionary&lt;string, object&gt;)"/> or
73+
/// Params(Func&lt;FluentDictionary&lt;string, object&gt;, FluentDictionary&lt;string, object&gt;&gt;)
74+
/// </remarks>
3375
public PhraseSuggestCollateDescriptor<T> Query(string script) => Assign(a => a.Query = (InlineScript)script);
3476

77+
/// <summary>
78+
/// The collate query to run.
79+
/// </summary>
80+
/// <remarks>
81+
/// Query parameters should be specified using <see cref="Params(IDictionary&lt;string, object&gt;)"/> or
82+
/// Params(Func&lt;FluentDictionary&lt;string, object&gt;, FluentDictionary&lt;string, object&gt;&gt;)
83+
/// </remarks>
3584
public PhraseSuggestCollateDescriptor<T> Query(Func<ScriptDescriptor, IScript> scriptSelector) =>
3685
Assign(a => a.Query = scriptSelector?.Invoke(new ScriptDescriptor()));
3786

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

src/Tests/Search/Request/SuggestUsageTests.cs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,10 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
4747
phrase = new {
4848
collate = new {
4949
query = new {
50-
inline = "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
51-
@params = new {
52-
field_name = "title"
53-
}
50+
inline = "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}"
51+
},
52+
@params = new {
53+
field_name = "title"
5454
},
5555
prune = true,
5656
},
@@ -121,8 +121,8 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
121121
.Collate(c => c
122122
.Query(q => q
123123
.Inline("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
124-
.Params(p => p.Add("field_name", "title"))
125124
)
125+
.Params(p => p.Add("field_name", "title"))
126126
.Prune()
127127
)
128128
.Confidence(10.1)
@@ -194,12 +194,10 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
194194
{
195195
Collate = new PhraseSuggestCollate
196196
{
197-
Query = new InlineScript("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
197+
Query = new InlineScript("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}"),
198+
Params = new Dictionary<string, object>
198199
{
199-
Params = new Dictionary<string, object>
200-
{
201-
{ "field_name", "title" }
202-
}
200+
{ "field_name", "title" }
203201
},
204202
Prune = true
205203
},

src/Tests/Search/Suggesters/SuggestApiTests.cs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ protected override LazyResponses ClientUsage() => Calls(
6060
collate = new {
6161
query = new {
6262
inline = "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
63-
@params = new {
64-
field_name = "title"
65-
}
63+
},
64+
@params = new {
65+
field_name = "title"
6666
},
6767
prune = true,
6868
},
@@ -131,8 +131,8 @@ protected override LazyResponses ClientUsage() => Calls(
131131
.Collate(c => c
132132
.Query(q => q
133133
.Inline("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
134-
.Params(p => p.Add("field_name", "title"))
135134
)
135+
.Params(p => p.Add("field_name", "title"))
136136
.Prune()
137137
)
138138
.Confidence(10.1)
@@ -195,12 +195,10 @@ protected override LazyResponses ClientUsage() => Calls(
195195
{
196196
Collate = new PhraseSuggestCollate
197197
{
198-
Query = new InlineScript("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
198+
Query = new InlineScript("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}"),
199+
Params = new Dictionary<string, object>
199200
{
200-
Params = new Dictionary<string, object>
201-
{
202-
{ "field_name", "title" }
203-
}
201+
{ "field_name", "title" }
204202
},
205203
Prune = true
206204
},

0 commit comments

Comments
 (0)