You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<p>For a good overview of what aggregations are, refer the <ahref="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html">original Elasticsearch docs</a> on the subject.</p>
4
4
<h2id="specifying-aggregations-during-search">Specifying Aggregations during Search</h2>
5
-
<p>Adding aggregations to a search request is as simple as</p>
5
+
<p>Adding aggregations to a search request is as simple as:</p>
6
+
<h4id="fluent-syntax">Fluent Syntax</h4>
6
7
<pre><code>var result = client.Search<ElasticsearchProject>(s => s
7
8
.Aggregations(a => a
8
9
.Terms("my_agg", st => st
@@ -12,7 +13,7 @@ <h2 id="specifying-aggregations-during-search">Specifying Aggregations during Se
12
13
)
13
14
)
14
15
);
15
-
</code></pre><p>The above can also be accomplished using the objectinitializersyntax (OIS)...</p>
Aggregations = new Dictionary<string, IAggregationContainer>
@@ -34,24 +35,25 @@ <h2 id="specifying-aggregations-during-search">Specifying Aggregations during Se
34
35
</code></pre><h3id="getting-to-your-aggregation">Getting to your aggregation</h3>
35
36
<p>The result of the aggregations are accessed from the <code>Aggs</code> property of the response using the key that was specified on the request, <code>my_agg</code>, in the above examples:</p>
</code></pre><p>Notice we executed a <ahref="/nest/aggregations/terms.html">terms aggregation</a>, and on the response we had to retrieve our results from the <code>Terms</code> property of <code>Aggs</code>. All aggregations work like this in NEST. If <code>my_agg</code> was a <ahref="/nest/aggregations/percentiles.html">percentiles aggregation</a> instead, we would have to extract the results from <code>Aggs.Percentiles</code></p>
38
+
</code></pre><p>Notice we executed a <ahref="/nest/aggregations/terms.html">terms aggregation</a>, and on the response we had to retrieve our results from the <code>Terms</code> property of <code>Aggs</code>. All aggregations work like this in NEST.</p>
39
+
<p>If <code>my_agg</code> was a <ahref="/nest/aggregations/percentiles.html">percentiles aggregation</a> instead, we would have to extract the results from <code>Aggs.Percentiles</code></p>
</code></pre><p>Or if it were a <ahref="/nest/aggregations/geohash-grid.html">geohash grid aggregation</a> we would retrieve it from <code>Aggs.GeoHash</code></p>
<p>Since aggregation response structures all fall into similar groups, each aggregation response in NEST is typed to a specific implementation of <code>IAggregationMetric</code>. This can be a <code>ValueMetric</code>, <code>SingleBucket</code>, <code>Bucket</code>, <code>BucketWithDocCount</code>, and the list goes on. The <code>Aggs</code> helper property of the response will automatically convert to the response from ES to the correct CLR type.</p>
43
45
<h2id="sub-aggregations">Sub-aggregations</h2>
44
-
<p>NEST of course also supports sub-aggregations...</p>
46
+
<p>NEST of course also supports sub-aggregations.</p>
47
+
<p>In the following example we are executing a <ahref="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html">terms aggregation</a>, <code>names</code>, as a top-level aggregation, and then within it a <ahref="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html">max aggregation</a>, <code>max_age</code>, as a sub-aggregation. This will produce a bucket per unique value of the <code>Name</code> field, and within each bucket find the max <code>Age</code> value for that particular name.</p>
45
48
<h4id="fluent-syntax">Fluent Syntax</h4>
46
-
<pre><code>var result = client.Search<ElasticsearchProject>(s => s
49
+
<pre><code>var result = client.Search<Person>(s => s
var mySubAgg = myAgg.Aggs.Max("my_sub_agg");
93
-
</code></pre><p>That's how NEST handles aggregations in a nutshell. Refer to the specific section on each aggregation type for more details.</p>
91
+
</code></pre><p>To access the <code>max_age</code> sub-aggregation, we first extract the top-level terms aggregation, <code>names</code>, from the response:</p>
Copy file name to clipboardExpand all lines: docs/contents/nest/aggregations/handling.markdown
+30-20Lines changed: 30 additions & 20 deletions
Original file line number
Diff line number
Diff line change
@@ -11,7 +11,9 @@ For a good overview of what aggregations are, refer the [original Elasticsearch
11
11
12
12
## Specifying Aggregations during Search
13
13
14
-
Adding aggregations to a search request is as simple as
14
+
Adding aggregations to a search request is as simple as:
15
+
16
+
#### Fluent Syntax
15
17
16
18
var result = client.Search<ElasticsearchProject>(s => s
17
19
.Aggregations(a => a
@@ -23,7 +25,7 @@ Adding aggregations to a search request is as simple as
23
25
)
24
26
);
25
27
26
-
The above can also be accomplished using the object initializer syntax (OIS)...
28
+
#### Object Initializer Syntax
27
29
28
30
var searchRequest = new SearchRequest
29
31
{
@@ -50,7 +52,9 @@ The result of the aggregations are accessed from the `Aggs` property of the resp
50
52
51
53
var myAgg = result.Aggs.Terms("my_agg");
52
54
53
-
Notice we executed a [terms aggregation](/nest/aggregations/terms.html), and on the response we had to retrieve our results from the `Terms` property of `Aggs`. All aggregations work like this in NEST. If `my_agg` was a [percentiles aggregation](/nest/aggregations/percentiles.html) instead, we would have to extract the results from `Aggs.Percentiles`
55
+
Notice we executed a [terms aggregation](/nest/aggregations/terms.html), and on the response we had to retrieve our results from the `Terms` property of `Aggs`. All aggregations work like this in NEST.
56
+
57
+
If `my_agg` was a [percentiles aggregation](/nest/aggregations/percentiles.html) instead, we would have to extract the results from `Aggs.Percentiles`
54
58
55
59
var myAgg = results.Aggs.Percentiles("my_agg");
56
60
@@ -64,19 +68,20 @@ Since aggregation response structures all fall into similar groups, each aggrega
64
68
65
69
## Sub-aggregations
66
70
67
-
NEST of course also supports sub-aggregations...
71
+
NEST of course also supports sub-aggregations.
72
+
73
+
In the following example we are executing a [terms aggregation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html), `names`, as a top-level aggregation, and then within it a [max aggregation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-max-aggregation.html), `max_age`, as a sub-aggregation. This will produce a bucket per unique value of the `Name` field, and within each bucket find the max `Age` value for that particular name.
68
74
69
75
#### Fluent Syntax
70
76
71
-
var result = client.Search<ElasticsearchProject>(s => s
0 commit comments