Skip to content

Commit f52bc9c

Browse files
committed
Merge branch '5.x' of github.com:elastic/elasticsearch-net into 5.x
2 parents b297e33 + f905270 commit f52bc9c

File tree

5 files changed

+147
-23
lines changed

5 files changed

+147
-23
lines changed

docs/aggregations/metric/top-hits/top-hits-aggregation-usage.asciidoc

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,18 @@ s => s
2828
.Aggregations(aa => aa
2929
.TopHits("top_state_hits", th => th
3030
.Sort(srt => srt
31-
.Field(p => p.StartedOn)
32-
.Order(SortOrder.Descending)
31+
.Field(sf => sf
32+
.Field(p => p.StartedOn)
33+
.Order(SortOrder.Descending)
34+
)
35+
.Script(ss => ss
36+
.Type("number")
37+
.Script(sss => sss
38+
.Inline("Math.sin(34*(double)doc['numberOfCommits'].value)")
39+
.Lang("painless")
40+
)
41+
.Order(SortOrder.Descending)
42+
)
3343
)
3444
.Source(src => src
3545
.Includes(fs => fs
@@ -79,7 +89,13 @@ new SearchRequest<Project>
7989
{
8090
Sort = new List<ISort>
8191
{
82-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
92+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
93+
new ScriptSort
94+
{
95+
Type = "number",
96+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
97+
Order = SortOrder.Descending
98+
},
8399
},
84100
Source = new SourceFilter
85101
{
@@ -127,6 +143,16 @@ new SearchRequest<Project>
127143
"startedOn": {
128144
"order": "desc"
129145
}
146+
},
147+
{
148+
"_script": {
149+
"type": "number",
150+
"script": {
151+
"lang": "painless",
152+
"inline": "Math.sin(34*(double)doc['numberOfCommits'].value)"
153+
},
154+
"order": "desc"
155+
}
130156
}
131157
],
132158
"_source": {

src/Nest/Aggregations/Metric/TopHits/TopHitsAggregation.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,17 @@ public class TopHitsAggregationDescriptor<T>
9595

9696
public TopHitsAggregationDescriptor<T> Size(int size) => Assign(a => a.Size = size);
9797

98+
[Obsolete("Use Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>>) that accepts multiple sort options")]
9899
public TopHitsAggregationDescriptor<T> Sort(Func<SortFieldDescriptor<T>, IFieldSort> sortSelector) => Assign(a =>
99100
{
100101
a.Sort = a.Sort ?? new List<ISort>();
101102
var sort = sortSelector?.Invoke(new SortFieldDescriptor<T>());
102103
if (sort != null) a.Sort.Add(sort);
103104
});
104105

106+
public TopHitsAggregationDescriptor<T> Sort(Func<SortDescriptor<T>, IPromise<IList<ISort>>> sortSelector) =>
107+
Assign(a => a.Sort = sortSelector?.Invoke(new SortDescriptor<T>())?.Value);
108+
105109
public TopHitsAggregationDescriptor<T> Source(bool enabled = true) =>
106110
Assign(a => a.Source = enabled);
107111

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/Aggregations/Metric/TopHits/TopHitsAggregationUsageTests.cs

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,19 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
3939
{
4040
order = "desc"
4141
}
42+
},
43+
new
44+
{
45+
_script = new
46+
{
47+
type = "number",
48+
script = new
49+
{
50+
lang = "painless",
51+
inline = "Math.sin(34*(double)doc['numberOfCommits'].value)"
52+
},
53+
order = "desc"
54+
}
4255
}
4356
},
4457
_source = new
@@ -84,8 +97,18 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
8497
.Aggregations(aa => aa
8598
.TopHits("top_state_hits", th => th
8699
.Sort(srt => srt
87-
.Field(p => p.StartedOn)
88-
.Order(SortOrder.Descending)
100+
.Field(sf => sf
101+
.Field(p => p.StartedOn)
102+
.Order(SortOrder.Descending)
103+
)
104+
.Script(ss => ss
105+
.Type("number")
106+
.Script(sss => sss
107+
.Inline("Math.sin(34*(double)doc['numberOfCommits'].value)")
108+
.Lang("painless")
109+
)
110+
.Order(SortOrder.Descending)
111+
)
89112
)
90113
.Source(src => src
91114
.Includes(fs => fs
@@ -131,7 +154,13 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
131154
{
132155
Sort = new List<ISort>
133156
{
134-
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending }
157+
new SortField { Field = Field<Project>(p => p.StartedOn), Order = SortOrder.Descending },
158+
new ScriptSort
159+
{
160+
Type = "number",
161+
Script = new InlineScript("Math.sin(34*(double)doc['numberOfCommits'].value)") { Lang = "painless" },
162+
Order = SortOrder.Descending
163+
},
135164
},
136165
Source = new SourceFilter
137166
{

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)