Skip to content

Commit 1d9ec08

Browse files
committed
Docs: Improve aggregation handling section (closes #1219)
1 parent c7c89a6 commit 1d9ec08

File tree

2 files changed

+55
-40
lines changed

2 files changed

+55
-40
lines changed

docs/build/nest/aggregations/handling.html

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
<script src="/scripts/html5shiv.js"></script><link rel="stylesheet" type="text/css" href="/styles/normalize.css"/><link rel="stylesheet" type="text/css" href="/styles/layout.css"/><link rel="stylesheet" type="text/css" href="/styles/pygments.css"/><link rel="stylesheet" type="text/css" href="/styles/pygments.css"/><link rel="stylesheet" type="text/css" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css"/><link href="//fonts.googleapis.com/css?family=Ubuntu+Mono|Open+Sans" rel="stylesheet" type="text/css"/><link href="/prettify/prettify.css" type="text/css" rel="stylesheet"/><link href="/prettify/sunburst.css" type="text/css" rel="stylesheet"/><script src="//code.jquery.com/jquery.min.js" type="text/javascript"></script><script type="text/javascript" src="/prettify/prettify.js"></script><script type="text/javascript" src="/prettify/fix_code_tags.js"></script></head><body><div class="wrapper"><header class="header"><div class="actions"><iframe src="//ghbtns.com/github-btn.html?user=elasticsearch&amp;repo=elasticsearch-net&amp;type=fork&amp;count=true" allowtransparency="true" frameborder="0" scrolling="0" width="95" height="20"></iframe><iframe src="//ghbtns.com/github-btn.html?user=elasticsearch&amp;repo=elasticsearch-net&amp;type=watch&amp;count=true" allowtransparency="true" frameborder="0" scrolling="0" width="110" height="20"></iframe></div><img src="/images/nest-nuget-icon.png" width="48" height="48"/><h1 class="nest">NEST</h1><p>Documentation</p></header><div class="divide"></div><div class="middle"><div class="container"><main class="content"><h1 id="aggregations">Aggregations</h1>
33
<p>For a good overview of what aggregations are, refer the <a href="http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations.html">original Elasticsearch docs</a> on the subject.</p>
44
<h2 id="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+
<h4 id="fluent-syntax">Fluent Syntax</h4>
67
<pre><code>var result = client.Search&lt;ElasticsearchProject&gt;(s =&gt; s
78
.Aggregations(a =&gt; a
89
.Terms(&quot;my_agg&quot;, st =&gt; st
@@ -12,7 +13,7 @@ <h2 id="specifying-aggregations-during-search">Specifying Aggregations during Se
1213
)
1314
)
1415
);
15-
</code></pre><p>The above can also be accomplished using the object initializer syntax (OIS)...</p>
16+
</code></pre><h4 id="object-initializer-syntax">Object Initializer Syntax</h4>
1617
<pre><code>var searchRequest = new SearchRequest
1718
{
1819
Aggregations = new Dictionary&lt;string, IAggregationContainer&gt;
@@ -34,24 +35,25 @@ <h2 id="specifying-aggregations-during-search">Specifying Aggregations during Se
3435
</code></pre><h3 id="getting-to-your-aggregation">Getting to your aggregation</h3>
3536
<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>
3637
<pre><code>var myAgg = result.Aggs.Terms(&quot;my_agg&quot;);
37-
</code></pre><p>Notice we executed a <a href="/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 <a href="/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 <a href="/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 <a href="/nest/aggregations/percentiles.html">percentiles aggregation</a> instead, we would have to extract the results from <code>Aggs.Percentiles</code></p>
3840
<pre><code>var myAgg = results.Aggs.Percentiles(&quot;my_agg&quot;);
3941
</code></pre><p>Or if it were a <a href="/nest/aggregations/geohash-grid.html">geohash grid aggregation</a> we would retrieve it from <code>Aggs.GeoHash</code></p>
4042
<pre><code>var myAgg = results.Aggs.GeoHash(&quot;my_agg&quot;)
4143
</code></pre><p>etc...</p>
4244
<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>
4345
<h2 id="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 <a href="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 <a href="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>
4548
<h4 id="fluent-syntax">Fluent Syntax</h4>
46-
<pre><code>var result = client.Search&lt;ElasticsearchProject&gt;(s =&gt; s
49+
<pre><code>var result = client.Search&lt;Person&gt;(s =&gt; s
4750
.Aggregations(a =&gt; a
48-
.Terms(&quot;my_agg&quot;, st =&gt; st
49-
.Field(o =&gt; o.Content)
51+
.Terms(&quot;names&quot;, st =&gt; st
52+
.Field(o =&gt; o.Name)
5053
.Size(10)
51-
.ExecutionHint(TermsAggregationExecutionHint.Ordinals)
5254
.Aggregations(aa =&gt; aa
53-
.Max(&quot;my_sub_agg&quot;, m =&gt; m
54-
.Field(o =&gt; o.LongValue)
55+
.Max(&quot;max_age&quot;, m =&gt; m
56+
.Field(o =&gt; o.Age)
5557
)
5658
)
5759
)
@@ -62,21 +64,20 @@ <h4 id="fluent-syntax">Fluent Syntax</h4>
6264
{
6365
Aggregations = new Dictionary&lt;string, IAggregationContainer&gt;
6466
{
65-
{ &quot;my_agg&quot;, new AggregationContainer
67+
{ &quot;names&quot;, new AggregationContainer
6668
{
6769
Terms = new TermsAggregator
6870
{
69-
Field = &quot;content&quot;,
70-
Size = 10,
71-
ExecutionHint = TermsAggregationExecutionHint.Ordinals
71+
Field = &quot;name&quot;,
72+
Size = 10
7273
},
7374
Aggregations = new Dictionary&lt;string, IAggregationContainer&gt;
7475
{
75-
{ &quot;my_sub_agg&quot;, new AggregationContainer
76+
{ &quot;max_age&quot;, new AggregationContainer
7677
{
7778
Max = new MaxAggregator
7879
{
79-
Field = &quot;longValue&quot;
80+
Field = &quot;age&quot;
8081
}
8182
}
8283
}
@@ -87,8 +88,12 @@ <h4 id="fluent-syntax">Fluent Syntax</h4>
8788
};
8889

8990
var result = client.Search&lt;ElasticsearchProject&gt;(searchRequest);
90-
</code></pre><p>Accessing the top level aggregation and sub-aggregation is pretty straight-forward...</p>
91-
<pre><code>var myAgg = result.Aggs.Terms(&quot;my_agg&quot;);
92-
var mySubAgg = myAgg.Aggs.Max(&quot;my_sub_agg&quot;);
93-
</code></pre><p>That&#39;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>
92+
<pre><code>var names = result.Aggs.Terms(&quot;names&quot;);
93+
</code></pre><p>We can then iterate over each <code>name</code> bucket and extract our <code>max_age</code> result:</p>
94+
<pre><code>foreach(var name in names.Items)
95+
{
96+
var maxAge = name.Aggs.Max(&quot;max_age&quot;);
97+
}
98+
</code></pre><p>That&#39;s aggregations in a nutshell. Refer to the specific section on each aggregation type for more details.</p>
9499
</main></div><aside class="left-sidebar"><aside id="menu"><ul><li><h4><a href="/">Home</a><a href="/contributing.html">Contributing</a><a href="/building.html">Building</a><a href="/breaking-changes.html">1.0 Breaking Changes</a><a href="https://github.com/elasticsearch/elasticsearch-net/releases">Release Notes</a></h4></li></ul><ul id="elasticsearch-net"><h4 class="title">Elasticsearch.Net</h4><ul><li><a href="/elasticsearch-net/quick-start.html">Quick Start</a></li><li><a href="/elasticsearch-net/connecting.html">Connecting</a></li><li><a href="/elasticsearch-net/security.html">Security</a></li><li><a href="/elasticsearch-net/cluster-failover.html">Cluster failover</a></li><li><a href="/elasticsearch-net/building-requests.html">Building requests</a></li><li><a href="/elasticsearch-net/handling-responses.html">Handling responses</a></li><li><a href="/elasticsearch-net/errors.html">Errors</a></li></ul></ul><ul id="nest"><h4 class="title">NEST</h4><ul><li><a href="/nest/quick-start.html">Quick Start</a></li><li><a href="/nest/connecting.html">Connecting</a></li><li><a href="/nest/index-type-inference.html">Type/Index Inference</a></li><li><a href="/nest/handling-responses.html">Handling responses</a></li><li><a href="/nest/writing-queries.html">Writing queries</a></li><li><a href="/nest/tips-tricks.html">Tips & Tricks</a></li></ul><li><h4><a href="/nest/core/"><i class="fa fa-chevron-right"></i>Core</a></h4></li><li><h4><a href="/nest/indices/aliases.html"><i class="fa fa-chevron-right"></i>Indices</a></h4></li><li><h4><a href="/nest/cluster/health.html"><i class="fa fa-chevron-right"></i>Cluster</a></h4></li><li><h4><a href="/nest/search/basics.html"><i class="fa fa-chevron-right"></i>Search</a></h4></li><h4><a href="/nest/aggregations/handling.html"><i class="fa fa-chevron-down"></i>Aggregations</a></h4><ul><li class="sub"><a href="/nest/aggregations/avg.html">Avg</a></li><li class="sub"><a href="/nest/aggregations/cardinality.html">Cardinality</a></li><li class="sub"><a href="/nest/aggregations/date-histogram.html">Date Histogram</a></li><li class="sub"><a href="/nest/aggregations/date-range.html">Date Range</a></li><li class="sub"><a href="/nest/aggregations/extended-stats.html">Extended Stats</a></li><li class="sub"><a href="/nest/aggregations/filter.html">Filter</a></li><li class="sub"><a href="/nest/aggregations/geo-bounds.html">Geo Bounds</a></li><li class="sub"><a href="/nest/aggregations/geo-distance.html">Geo Distance</a></li><li class="sub"><a href="/nest/aggregations/geohash-grid.html">Geohash Grid</a></li><li class="sub"><a href="/nest/aggregations/global.html">Global</a></li><li class="sub"><a href="/nest/aggregations/histogram.html">Histogram</a></li><li class="sub"><a href="/nest/aggregations/ipv4.html">IPv4 Range</a></li><li class="sub"><a href="/nest/aggregations/max.html">Max</a></li><li class="sub"><a href="/nest/aggregations/min.html">Min</a></li><li class="sub"><a href="/nest/aggregations/missing.html">Missing</a></li><li class="sub"><a href="/nest/aggregations/percentiles.html">Percentiles</a></li><li class="sub"><a href="/nest/aggregations/percentile-ranks.html">Percentiles Ranks</a></li><li class="sub"><a href="/nest/aggregations/range.html">Range</a></li><li class="sub"><a href="/nest/aggregations/nested.html">Nested</a></li><li class="sub"><a href="/nest/aggregations/reverse-nested.html">Reverse Nested</a></li><li class="sub"><a href="/nest/aggregations/significant-terms.html">Significant Terms</a></li><li class="sub"><a href="/nest/aggregations/stats.html">Stats</a></li><li class="sub"><a href="/nest/aggregations/sum.html">Sum</a></li><li class="sub"><a href="/nest/aggregations/terms.html">Terms</a></li><li class="sub"><a href="/nest/aggregations/top-hits.html">Top Hits</a></li><li class="sub"><a href="/nest/aggregations/value-count.html">Value Count</a></li></ul><li><h4><a href="/nest/facets/handling.html" class="selected"><i class="fa fa-chevron-right"></i>Facets</a></h4></li></ul></aside></aside></div><footer class="footer"></footer></div></body></html>

docs/contents/nest/aggregations/handling.markdown

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,9 @@ For a good overview of what aggregations are, refer the [original Elasticsearch
1111

1212
## Specifying Aggregations during Search
1313

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
1517

1618
var result = client.Search<ElasticsearchProject>(s => s
1719
.Aggregations(a => a
@@ -23,7 +25,7 @@ Adding aggregations to a search request is as simple as
2325
)
2426
);
2527

26-
The above can also be accomplished using the object initializer syntax (OIS)...
28+
#### Object Initializer Syntax
2729

2830
var searchRequest = new SearchRequest
2931
{
@@ -50,7 +52,9 @@ The result of the aggregations are accessed from the `Aggs` property of the resp
5052

5153
var myAgg = result.Aggs.Terms("my_agg");
5254

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`
5458

5559
var myAgg = results.Aggs.Percentiles("my_agg");
5660

@@ -64,19 +68,20 @@ Since aggregation response structures all fall into similar groups, each aggrega
6468

6569
## Sub-aggregations
6670

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.
6874

6975
#### Fluent Syntax
7076

71-
var result = client.Search<ElasticsearchProject>(s => s
77+
var result = client.Search<Person>(s => s
7278
.Aggregations(a => a
73-
.Terms("my_agg", st => st
74-
.Field(o => o.Content)
79+
.Terms("names", st => st
80+
.Field(o => o.Name)
7581
.Size(10)
76-
.ExecutionHint(TermsAggregationExecutionHint.Ordinals)
7782
.Aggregations(aa => aa
78-
.Max("my_sub_agg", m => m
79-
.Field(o => o.LongValue)
83+
.Max("max_age", m => m
84+
.Field(o => o.Age)
8085
)
8186
)
8287
)
@@ -89,21 +94,20 @@ NEST of course also supports sub-aggregations...
8994
{
9095
Aggregations = new Dictionary<string, IAggregationContainer>
9196
{
92-
{ "my_agg", new AggregationContainer
97+
{ "names", new AggregationContainer
9398
{
9499
Terms = new TermsAggregator
95100
{
96-
Field = "content",
97-
Size = 10,
98-
ExecutionHint = TermsAggregationExecutionHint.Ordinals
101+
Field = "name",
102+
Size = 10
99103
},
100104
Aggregations = new Dictionary<string, IAggregationContainer>
101105
{
102-
{ "my_sub_agg", new AggregationContainer
106+
{ "max_age", new AggregationContainer
103107
{
104108
Max = new MaxAggregator
105109
{
106-
Field = "longValue"
110+
Field = "age"
107111
}
108112
}
109113
}
@@ -115,9 +119,15 @@ NEST of course also supports sub-aggregations...
115119

116120
var result = client.Search<ElasticsearchProject>(searchRequest);
117121

118-
Accessing the top level aggregation and sub-aggregation is pretty straight-forward...
122+
To access the `max_age` sub-aggregation, we first extract the top-level terms aggregation, `names`, from the response:
119123

120-
var myAgg = result.Aggs.Terms("my_agg");
121-
var mySubAgg = myAgg.Aggs.Max("my_sub_agg");
124+
var names = result.Aggs.Terms("names");
125+
126+
We can then iterate over each `name` bucket and extract our `max_age` result:
127+
128+
foreach(var name in names.Items)
129+
{
130+
var maxAge = name.Aggs.Max("max_age");
131+
}
122132

123-
That's how NEST handles aggregations in a nutshell. Refer to the specific section on each aggregation type for more details.
133+
That's aggregations in a nutshell. Refer to the specific section on each aggregation type for more details.

0 commit comments

Comments
 (0)