Skip to content

Commit 236ebbf

Browse files
committed
Add Params to PhraseSuggestCollate
This commit adds a Parameters dictionary to IPhraseSuggestCollate so that parameters are serialized to the correct query form and introduces a PhraseSuggestCollateQuery to specify the query. Add XML documentation to indicate the usage of each property. Closes #2849 (cherry-picked from commit f905270)
1 parent 65bc7d7 commit 236ebbf

File tree

4 files changed

+118
-26
lines changed

4 files changed

+118
-26
lines changed

docs/search/request/suggest-usage.asciidoc

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@ s => s
6262
.Collate(c => c
6363
.Query(q => q
6464
.Source("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
65-
.Params(p => p.Add("field_name", "title"))
6665
)
66+
.Params(p => p.Add("field_name", "title"))
6767
.Prune()
6868
)
6969
.Confidence(10.1)
@@ -134,13 +134,13 @@ new SearchRequest<Project>
134134
{
135135
Collate = new PhraseSuggestCollate
136136
{
137-
Query = new TemplateQuery
137+
Query = new PhraseSuggestCollateQuery
138138
{
139139
Source = "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
140-
Params = new Dictionary<string, object>
141-
{
142-
{ "field_name", "title" }
143-
}
140+
},
141+
Params = new Dictionary<string, object>
142+
{
143+
{ "field_name", "title" }
144144
},
145145
Prune = true
146146
},
@@ -189,10 +189,10 @@ new SearchRequest<Project>
189189
"phrase": {
190190
"collate": {
191191
"query": {
192-
"source": "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
193-
"params": {
194-
"field_name": "title"
195-
}
192+
"source": "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}"
193+
},
194+
"params": {
195+
"field_name": "title"
196196
},
197197
"prune": true
198198
},

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

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,77 @@
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
{
15+
/// <summary>
16+
/// The collate query to run.
17+
/// </summary>
1118
[JsonProperty("query")]
12-
ITemplateQuery Query { get; set; }
19+
IPhraseSuggestCollateQuery Query { get; set; }
1320

21+
/// <summary>
22+
/// Controls if all phrase suggestions will be returned. When set to <c>true</c>, the suggestions will have
23+
/// an additional option collate_match, which will be <c>true</c> if matching documents for the phrase was found,
24+
/// <c>false</c> otherwise. The default value for <see cref="Prune"/> is <c>false</c>.
25+
/// </summary>
1426
[JsonProperty("prune")]
1527
bool? Prune { get; set; }
28+
29+
/// <summary>
30+
/// The parameters for the query. the suggestion value will be added to the variables you specify.
31+
/// </summary>
32+
[JsonProperty("params")]
33+
IDictionary<string, object> Params { get; set; }
1634
}
1735

36+
/// <inheritdoc />
1837
public class PhraseSuggestCollate : IPhraseSuggestCollate
1938
{
20-
public ITemplateQuery Query { get; set; }
39+
/// <inheritdoc />
40+
public IPhraseSuggestCollateQuery Query { get; set; }
2141

42+
/// <inheritdoc />
2243
public bool? Prune { get; set; }
44+
45+
/// <inheritdoc />
46+
public IDictionary<string, object> Params { get; set; }
2347
}
2448

2549
public class PhraseSuggestCollateDescriptor<T> : DescriptorBase<PhraseSuggestCollateDescriptor<T>, IPhraseSuggestCollate>, IPhraseSuggestCollate
2650
where T : class
2751
{
28-
ITemplateQuery IPhraseSuggestCollate.Query { get; set; }
29-
52+
IPhraseSuggestCollateQuery IPhraseSuggestCollate.Query { get; set; }
53+
IDictionary<string, object> IPhraseSuggestCollate.Params { get; set; }
3054
bool? IPhraseSuggestCollate.Prune { get; set; }
3155

32-
public PhraseSuggestCollateDescriptor<T> Query(Func<TemplateQueryDescriptor<T>, ITemplateQuery> selector) =>
33-
Assign(a => a.Query = selector?.Invoke(new TemplateQueryDescriptor<T>()));
56+
/// <summary>
57+
/// The collate query to run
58+
/// </summary>
59+
public PhraseSuggestCollateDescriptor<T> Query(Func<PhraseSuggestCollateQueryDescriptor, IPhraseSuggestCollateQuery> selector) =>
60+
Assign(a => a.Query = selector?.Invoke(new PhraseSuggestCollateQueryDescriptor()));
3461

62+
/// <summary>
63+
/// Controls if all phrase suggestions will be returned. When set to <c>true</c>, the suggestions will have
64+
/// an additional option collate_match, which will be <c>true</c> if matching documents for the phrase was found,
65+
/// <c>false</c> otherwise. The default value for <see cref="Prune"/> is <c>false</c>.
66+
/// </summary>
3567
public PhraseSuggestCollateDescriptor<T> Prune(bool? prune = true) => Assign(a => a.Prune = prune);
68+
69+
/// <summary>
70+
/// The parameters for the query. the suggestion value will be added to the variables you specify.
71+
/// </summary>
72+
public PhraseSuggestCollateDescriptor<T> Params(IDictionary<string, object> paramsDictionary) => Assign(a => a.Params = paramsDictionary);
73+
74+
/// <summary>
75+
/// The parameters for the query. the suggestion value will be added to the variables you specify.
76+
/// </summary>
77+
public PhraseSuggestCollateDescriptor<T> Params(Func<FluentDictionary<string, object>, FluentDictionary<string, object>> paramsDictionary) =>
78+
Assign(a => a.Params = paramsDictionary(new FluentDictionary<string, object>()));
3679
}
3780
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
using Newtonsoft.Json;
2+
3+
namespace Nest
4+
{
5+
/// <summary>
6+
/// A query to run for a phrase suggester collate
7+
/// </summary>
8+
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
9+
[JsonConverter(typeof(ReadAsTypeJsonConverter<PhraseSuggestCollateQuery>))]
10+
public interface IPhraseSuggestCollateQuery
11+
{
12+
/// <summary>
13+
/// The source script to be executed
14+
/// </summary>
15+
[JsonProperty("source")]
16+
string Source { get; set; }
17+
18+
/// <summary>
19+
/// The id for a stored script to execute
20+
/// </summary>
21+
[JsonProperty("id")]
22+
Id Id { get; set; }
23+
}
24+
25+
/// <inheritdoc />
26+
public class PhraseSuggestCollateQuery : IPhraseSuggestCollateQuery
27+
{
28+
/// <inheritdoc />
29+
public string Source { get; set; }
30+
31+
/// <inheritdoc />
32+
public Id Id { get; set; }
33+
}
34+
35+
/// <inheritdoc />
36+
public class PhraseSuggestCollateQueryDescriptor :
37+
DescriptorBase<PhraseSuggestCollateQueryDescriptor, IPhraseSuggestCollateQuery>, IPhraseSuggestCollateQuery
38+
{
39+
string IPhraseSuggestCollateQuery.Source { get; set; }
40+
Id IPhraseSuggestCollateQuery.Id { get; set; }
41+
42+
/// <inheritdoc />
43+
public PhraseSuggestCollateQueryDescriptor Source(string source) => Assign(a => a.Source = source);
44+
45+
/// <inheritdoc />
46+
public PhraseSuggestCollateQueryDescriptor Id(Id id) => Assign(a => a.Id = id);
47+
}
48+
}

src/Tests/Search/Request/SuggestUsageTests.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,9 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
5050
collate = new {
5151
query = new {
5252
source = "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
53-
@params = new {
54-
field_name = "title"
55-
}
53+
},
54+
@params = new {
55+
field_name = "title"
5656
},
5757
prune = true,
5858
},
@@ -81,7 +81,8 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
8181
suggest_mode = "always"
8282
},
8383
text = "hello world"
84-
} }
84+
}
85+
}
8586
}
8687
};
8788

@@ -123,8 +124,8 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
123124
.Collate(c => c
124125
.Query(q => q
125126
.Source("{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}")
126-
.Params(p => p.Add("field_name", "title"))
127127
)
128+
.Params(p => p.Add("field_name", "title"))
128129
.Prune()
129130
)
130131
.Confidence(10.1)
@@ -190,13 +191,13 @@ public SuggestUsageTests(ReadOnlyCluster cluster, EndpointUsage usage) : base(cl
190191
{
191192
Collate = new PhraseSuggestCollate
192193
{
193-
Query = new TemplateQuery
194+
Query = new PhraseSuggestCollateQuery
194195
{
195196
Source = "{ \"match\": { \"{{field_name}}\": \"{{suggestion}}\" }}",
196-
Params = new Dictionary<string, object>
197-
{
198-
{ "field_name", "title" }
199-
}
197+
},
198+
Params = new Dictionary<string, object>
199+
{
200+
{ "field_name", "title" }
200201
},
201202
Prune = true
202203
},

0 commit comments

Comments
 (0)