Skip to content

Commit b50b099

Browse files
authored
Add DocValueFields to TopHits Aggregation (#3292)
closes #3274
1 parent 4c783bc commit b50b099

File tree

4 files changed

+48
-17
lines changed

4 files changed

+48
-17
lines changed

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

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,15 @@ a => a
6161
.ScriptField("commit_factor", sf => sf
6262
.Source("doc['numberOfCommits'].value * 2")
6363
64+
)
65+
66+
)
67+
.DocValueFields(d => d
68+
.Field(p => p.State)
6469
)
6570
)
6671
)
6772
)
68-
)
6973
----
7074

7175
==== Object Initializer syntax example
@@ -104,15 +108,17 @@ new TermsAggregation("states")
104108
{ Field<Project>(p => p.Description), new HighlightField() }
105109
}
106110
},
107-
ScriptFields = new ScriptFields{
111+
ScriptFields = new ScriptFields
108112
{
109-
"commit_factor", new ScriptField
110113
{
111-
Script = new InlineScript("doc['numberOfCommits'].value * 2")
112-
}
113-
}
114+
"commit_factor", new ScriptField
115+
{
116+
Script = new InlineScript("doc['numberOfCommits'].value * 2")
117+
}
118+
},
119+
},
120+
DocValueFields = Infer.Fields<Project>(f => f.State)
114121
}
115-
}
116122
}
117123
----
118124

@@ -170,7 +176,10 @@ new TermsAggregation("states")
170176
"source": "doc['numberOfCommits'].value * 2"
171177
}
172178
}
173-
}
179+
},
180+
"docvalue_fields": [
181+
"state"
182+
]
174183
}
175184
}
176185
}

src/CodeGeneration/DocGenerator/StringExtensions.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,10 @@ public static string[] SplitOnNewLines(this string input, StringSplitOptions opt
210210
}" },
211211
{ "LineStringCoordinates", @"new [] { new [] {38.897676, -77.03653}, new [] {38.889939, -77.009051} }" },
212212
{ "PointCoordinates", "new[] { 38.897676, -77.03653 }" },
213+
{ "this._polygonCoordinates", @"new[]{
214+
new []{ new [] {10.0, -17.0}, new [] {15.0, 16.0}, new [] {0.0, 12.0}, new [] {-15.0, 16.0}, new [] {-10.0, -17.0},new [] {10.0, -17.0}},
215+
new []{ new [] {8.2, 18.2}, new [] {8.2, -18.8}, new [] {-8.8, -10.8}, new [] {8.8, 18.2}}
216+
}" },
213217
};
214218

215219
public static bool TryGetJsonForAnonymousType(this string anonymousTypeString, out string json)

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ public interface ITopHitsAggregation : IMetricAggregation
3333
[JsonProperty("stored_fields")]
3434
Fields StoredFields { get; set; }
3535

36+
[JsonProperty("docvalue_fields")]
37+
Fields DocValueFields { get; set; }
38+
3639
[JsonProperty("version")]
3740
bool? Version { get; set; }
3841

@@ -52,6 +55,7 @@ public class TopHitsAggregation : MetricAggregationBase, ITopHitsAggregation
5255
public Fields StoredFields { get; set; }
5356
public bool? Version { get; set; }
5457
public bool? TrackScores { get; set; }
58+
public Fields DocValueFields { get; set; }
5559

5660
internal TopHitsAggregation() { }
5761

@@ -85,6 +89,8 @@ public class TopHitsAggregationDescriptor<T>
8589

8690
bool? ITopHitsAggregation.TrackScores { get; set; }
8791

92+
Fields ITopHitsAggregation.DocValueFields { get; set; }
93+
8894
public TopHitsAggregationDescriptor<T> From(int? from) => Assign(a => a.From = from);
8995

9096
public TopHitsAggregationDescriptor<T> Size(int? size) => Assign(a => a.Size = size);
@@ -112,5 +118,10 @@ public TopHitsAggregationDescriptor<T> StoredFields(Func<FieldsDescriptor<T>, IP
112118
public TopHitsAggregationDescriptor<T> Version(bool? version = true) => Assign(a => a.Version = version);
113119

114120
public TopHitsAggregationDescriptor<T> TrackScores(bool? trackScores = true) => Assign(a => a.TrackScores = trackScores);
121+
122+
public TopHitsAggregationDescriptor<T> DocValueFields(Func<FieldsDescriptor<T>, IPromise<Fields>> fields) =>
123+
Assign(a => a.DocValueFields = fields?.Invoke(new FieldsDescriptor<T>())?.Value);
124+
125+
public TopHitsAggregationDescriptor<T> DocValueFields(Fields fields) => Assign(a => a.DocValueFields = fields);
115126
}
116127
}

src/Tests/Aggregations/Metric/TopHits/TopHitsAggregationUsageTests.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
7878
source = "doc['numberOfCommits'].value * 2",
7979
}
8080
}
81-
}
81+
},
82+
docvalue_fields = new [] { "state" }
8283
}
8384
}
8485
}
@@ -127,11 +128,15 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
127128
.ScriptField("commit_factor", sf => sf
128129
.Source("doc['numberOfCommits'].value * 2")
129130

131+
)
132+
133+
)
134+
.DocValueFields(d => d
135+
.Field(p => p.State)
130136
)
131137
)
132138
)
133-
)
134-
);
139+
);
135140

136141
protected override AggregationDictionary InitializerAggs =>
137142
new TermsAggregation("states")
@@ -166,15 +171,17 @@ public TopHitsAggregationUsageTests(ReadOnlyCluster i, EndpointUsage usage) : ba
166171
{ Field<Project>(p => p.Description), new HighlightField() }
167172
}
168173
},
169-
ScriptFields = new ScriptFields{
174+
ScriptFields = new ScriptFields
170175
{
171-
"commit_factor", new ScriptField
172176
{
173-
Script = new InlineScript("doc['numberOfCommits'].value * 2")
174-
}
175-
}
177+
"commit_factor", new ScriptField
178+
{
179+
Script = new InlineScript("doc['numberOfCommits'].value * 2")
180+
}
181+
},
182+
},
183+
DocValueFields = Infer.Fields<Project>(f => f.State)
176184
}
177-
}
178185
};
179186

180187
protected override void ExpectResponse(ISearchResponse<Project> response)

0 commit comments

Comments
 (0)