Skip to content

Commit 8f07d14

Browse files
authored
Add Params to PhraseSuggestCollate (forward port to master) (#2977)
* 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) * Deprecate all usages of TemplateQuery Now that Phrase suggester no longer uses TemplateQuery, deprecate the interface, object initializer and fluent descriptor of template query
1 parent 3870f59 commit 8f07d14

File tree

10 files changed

+142
-38
lines changed

10 files changed

+142
-38
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/QueryDsl/Abstractions/Container/IQueryContainer.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,8 +137,10 @@ public interface IQueryContainer
137137
[JsonProperty("function_score")]
138138
IFunctionScoreQuery FunctionScore { get; set; }
139139

140+
#pragma warning disable 618
140141
[JsonProperty("template")]
141142
ITemplateQuery Template { get; set; }
143+
#pragma warning restore 618
142144

143145
[JsonProperty("geo_bounding_box")]
144146
IGeoBoundingBoxQuery GeoBoundingBox { get; set; }

src/Nest/QueryDsl/Abstractions/Container/QueryContainer-Assignments.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public partial class QueryContainer : IQueryContainer, IDescriptor
4646
private IIndicesQuery _indices;
4747
#pragma warning restore 618
4848
private IFunctionScoreQuery _functionScore;
49+
#pragma warning disable 618
4950
private ITemplateQuery _template;
51+
#pragma warning restore 618
5052
private IGeoBoundingBoxQuery _geoBoundingBox;
5153
private IGeoDistanceQuery _geoDistance;
5254
private IGeoPolygonQuery _geoPolygon;
@@ -110,7 +112,9 @@ private T Set<T>(T value) where T : IQuery
110112
IIndicesQuery IQueryContainer.Indices { get { return _indices; } set { _indices = Set(value); } }
111113
#pragma warning restore 618
112114
IFunctionScoreQuery IQueryContainer.FunctionScore { get { return _functionScore; } set { _functionScore = Set(value); } }
115+
#pragma warning disable 618
113116
ITemplateQuery IQueryContainer.Template { get { return _template; } set { _template = Set(value); } }
117+
#pragma warning restore 618
114118
IGeoBoundingBoxQuery IQueryContainer.GeoBoundingBox { get { return _geoBoundingBox; } set { _geoBoundingBox = Set(value); } }
115119
IGeoDistanceQuery IQueryContainer.GeoDistance { get { return _geoDistance; } set { _geoDistance = Set(value); } }
116120
IGeoPolygonQuery IQueryContainer.GeoPolygon { get { return _geoPolygon; } set { _geoPolygon = Set(value); } }

src/Nest/QueryDsl/Specialized/Template/TemplateQuery.cs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,11 @@
44

55
namespace Nest
66
{
7+
[Obsolete("Deprecated in 5.0.0. Use Search Template API instead")]
78
[JsonObject(MemberSerialization = MemberSerialization.OptIn)]
89
[JsonConverter(typeof(ReadAsTypeJsonConverter<TemplateQuery>))]
910
public interface ITemplateQuery : IQuery
1011
{
11-
[JsonProperty("file")]
12-
string File { get; set; }
13-
1412
[JsonProperty("source")]
1513
string Source { get; set; }
1614

@@ -25,38 +23,42 @@ public interface ITemplateQuery : IQuery
2523
IDictionary<string, object> Params { get; set; }
2624
}
2725

26+
[Obsolete("Deprecated in 5.0.0. Use Search Template API instead")]
2827
public class TemplateQuery : QueryBase, ITemplateQuery
2928
{
3029
protected override bool Conditionless => IsConditionless(this);
31-
public string File { get; set; }
30+
3231
/// <summary> An inline script to be executed </summary>
3332
public string Source { get; set; }
33+
3434
/// <summary> An inline script to be executed </summary>
3535
[Obsolete("Inline is being deprecated for Source and will be removed in Elasticsearch 7.0")]
3636
public string Inline { get => this.Source; set => this.Source = value; }
37+
3738
public Id Id { get; set; }
39+
3840
public IDictionary<string, object> Params { get; set;}
3941

4042
internal override void InternalWrapInContainer(IQueryContainer c) => c.Template = this;
41-
internal static bool IsConditionless(ITemplateQuery q) => q.File.IsNullOrEmpty() && q.Id == null && q.Source.IsNullOrEmpty();
43+
internal static bool IsConditionless(ITemplateQuery q) => q.Id == null && q.Source.IsNullOrEmpty();
4244
}
4345

46+
[Obsolete("Deprecated in 5.0.0. Use Search Template API instead")]
4447
public class TemplateQueryDescriptor<T>
4548
: QueryDescriptorBase<TemplateQueryDescriptor<T>, ITemplateQuery>
4649
, ITemplateQuery where T : class
4750
{
4851
protected override bool Conditionless => TemplateQuery.IsConditionless(this);
49-
string ITemplateQuery.File { get; set; }
52+
5053
string ITemplateQuery.Source { get; set; }
5154
string ITemplateQuery.Inline { get => Self.Source; set => Self.Source = value; }
5255
Id ITemplateQuery.Id { get; set; }
5356
IDictionary<string, object> ITemplateQuery.Params { get; set; }
5457

5558
[Obsolete("Inline is being deprecated for Source and will be removed in Elasticsearch 7.0")]
5659
public TemplateQueryDescriptor<T> Inline(string script) => Assign(a => a.Inline = script);
57-
public TemplateQueryDescriptor<T> Source(string script) => Assign(a => a.Source = script);
5860

59-
public TemplateQueryDescriptor<T> File(string file) => Assign(a => a.File = file);
61+
public TemplateQueryDescriptor<T> Source(string script) => Assign(a => a.Source = script);
6062

6163
public TemplateQueryDescriptor<T> Id(Id id) => Assign(a => a.Id = id);
6264

src/Nest/QueryDsl/Visitor/DslPrettyPrintVisitor.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,9 @@ private void Write(string queryType, Field field = null)
155155

156156
public virtual void Visit(IGeoHashCellQuery filter) => Write("geohash_cell");
157157

158+
#pragma warning disable 618
158159
public virtual void Visit(ITemplateQuery query) => Write("template");
160+
#pragma warning restore 618
159161

160162
public virtual void Visit(ISpanMultiTermQuery query) => Write("span_multi_term");
161163

src/Nest/QueryDsl/Visitor/QueryVisitor.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ public interface IQueryVisitor
6666
void Visit(IDateRangeQuery query);
6767
void Visit(INumericRangeQuery query);
6868
void Visit(ITermRangeQuery query);
69+
#pragma warning disable 618
6970
void Visit(ITemplateQuery query);
71+
#pragma warning restore 618
7072
void Visit(ISpanFirstQuery query);
7173
void Visit(ISpanNearQuery query);
7274
void Visit(ISpanNotQuery query);
@@ -209,7 +211,9 @@ public virtual void Visit(IGeoDistanceQuery query) { }
209211

210212
public virtual void Visit(IGeoHashCellQuery query) { }
211213

214+
#pragma warning disable 618
212215
public virtual void Visit(ITemplateQuery query) { }
216+
#pragma warning restore 618
213217

214218
public virtual void Visit(IGeoShapeMultiPointQuery query) { }
215219

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/QueryDsl/Specialized/Template/TemplateQueryUsageTests.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ public TemplateQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i,
2626
}
2727
};
2828

29+
#pragma warning disable 618
2930
protected override QueryContainer QueryInitializer => new TemplateQuery
3031
{
3132
Name = "named_query",
@@ -37,28 +38,25 @@ public TemplateQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i,
3738
}
3839
};
3940

40-
#pragma warning disable 618
4141
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q
4242
.Template(sn => sn
4343
.Name("named_query")
4444
.Boost(1.1)
4545
.Source(_templateString)
4646
.Params(p=>p.Add("query_string", "all about search"))
4747
);
48-
#pragma warning restore 618
4948

5049
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<ITemplateQuery>(a => a.Template)
5150
{
5251
q => {
5352
q.Source = "";
5453
q.Id = null;
55-
q.File = "";
5654
},
5755
q => {
5856
q.Source = null;
5957
q.Id = null;
60-
q.File = null;
6158
}
6259
};
6360
}
61+
#pragma warning restore 618
6462
}

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)