- .NET Clients: other versions:
- Introduction
- Breaking changes
- API Conventions
- Elasticsearch.Net - Low level client
- NEST - High level client
- Troubleshooting
- Search
- Query DSL
- Full text queries
- Term level queries
- Exists Query Usage
- Fuzzy Date Query Usage
- Fuzzy Numeric Query Usage
- Fuzzy Query Usage
- Ids Query Usage
- Prefix Query Usage
- Date Range Query Usage
- Numeric Range Query Usage
- Term Range Query Usage
- Regexp Query Usage
- Term Query Usage
- Terms List Query Usage
- Terms Lookup Query Usage
- Terms Query Usage
- Type Query Usage
- Wildcard Query Usage
- Compound queries
- Joining queries
- Geo queries
- Geo Bounding Box Query Usage
- Geo Distance Query Usage
- Geo Distance Range Query Usage
- Geo Hash Cell Query Usage
- Geo Polygon Query Usage
- Geo Shape Circle Query Usage
- Geo Shape Envelope Query Usage
- Geo Shape Geometry Collection Query Usage
- Geo Shape Indexed Shape Query Usage
- Geo Shape Line String Query Usage
- Geo Shape Multi Line String Query Usage
- Geo Shape Multi Point Query Usage
- Geo Shape Multi Polygon Query Usage
- Geo Shape Point Query Usage
- Geo Shape Polygon Query Usage
- Specialized queries
- Span queries
- NEST specific queries
- Aggregations
- Metric Aggregations
- Average Aggregation Usage
- Cardinality Aggregation Usage
- Extended Stats Aggregation Usage
- Geo Bounds Aggregation Usage
- Geo Centroid Aggregation Usage
- Max Aggregation Usage
- Min Aggregation Usage
- Percentile Ranks Aggregation Usage
- Percentiles Aggregation Usage
- Scripted Metric Aggregation Usage
- Stats Aggregation Usage
- Sum Aggregation Usage
- Top Hits Aggregation Usage
- Value Count Aggregation Usage
- Bucket Aggregations
- Adjacency Matrix Usage
- Children Aggregation Usage
- Date Histogram Aggregation Usage
- Date Range Aggregation Usage
- Filter Aggregation Usage
- Filters Aggregation Usage
- Geo Distance Aggregation Usage
- Geo Hash Grid Aggregation Usage
- Global Aggregation Usage
- Histogram Aggregation Usage
- Ip Range Aggregation Usage
- Missing Aggregation Usage
- Nested Aggregation Usage
- Range Aggregation Usage
- Reverse Nested Aggregation Usage
- Sampler Aggregation Usage
- Significant Terms Aggregation Usage
- Terms Aggregation Usage
- Pipeline Aggregations
- Average Bucket Aggregation Usage
- Bucket Script Aggregation Usage
- Bucket Selector Aggregation Usage
- Cumulative Sum Aggregation Usage
- Derivative Aggregation Usage
- Extended Stats Bucket Aggregation Usage
- Max Bucket Aggregation Usage
- Min Bucket Aggregation Usage
- Moving Average Ewma Aggregation Usage
- Moving Average Holt Linear Aggregation Usage
- Moving Average Holt Winters Aggregation Usage
- Moving Average Linear Aggregation Usage
- Moving Average Simple Aggregation Usage
- Percentiles Bucket Aggregation Usage
- Serial Differencing Aggregation Usage
- Stats Bucket Aggregation Usage
- Sum Bucket Aggregation Usage
- Matrix Aggregations
- Metric Aggregations
WARNING: Version 5.x has passed its EOL date.
This documentation is no longer being maintained and may be removed. If you are running this version, we strongly advise you to upgrade. For the latest information, see the current release documentation.
Terms Aggregation Usage
editTerms Aggregation Usage
editA multi-bucket value source based aggregation where buckets are dynamically built - one per unique value.
See the Elasticsearch documentation on terms aggregation for more detail.
Fluent DSL example
edits => s .Size(0) .Aggregations(a => a .Terms("states", st => st .Field(p => p.State) .MinimumDocumentCount(2) .Size(5) .ShardSize(100) .ExecutionHint(TermsAggregationExecutionHint.Map) .Missing("n/a") .Script(ss => ss.Inline("'State of Being: '+_value").Lang("groovy")) .Order(TermsOrder.TermAscending) .Order(TermsOrder.CountDescending) .Meta(m => m .Add("foo", "bar") ) ) )
Object Initializer syntax example
editnew SearchRequest<Project> { Size = 0, Aggregations = new TermsAggregation("states") { Field = Field<Project>(p => p.State), MinimumDocumentCount = 2, Size = 5, ShardSize = 100, ExecutionHint = TermsAggregationExecutionHint.Map, Missing = "n/a", Script = new InlineScript("'State of Being: '+_value") { Lang = "groovy" }, Order = new List<TermsOrder> { TermsOrder.TermAscending, TermsOrder.CountDescending }, Meta = new Dictionary<string, object> { { "foo", "bar" } } } }
Example json output.
{ "size": 0, "aggs": { "states": { "meta": { "foo": "bar" }, "terms": { "field": "state", "min_doc_count": 2, "size": 5, "shard_size": 100, "execution_hint": "map", "missing": "n/a", "script": { "inline": "'State of Being: '+_value", "lang": "groovy" }, "order": [ { "_term": "asc" }, { "_count": "desc" } ] } } } }
Handling Responses
editresponse.ShouldBeValid(); var states = response.Aggs.Terms("states"); states.Should().NotBeNull(); states.DocCountErrorUpperBound.Should().HaveValue(); states.SumOtherDocCount.Should().HaveValue(); states.Buckets.Should().NotBeNull(); states.Buckets.Count.Should().BeGreaterThan(0); foreach (var item in states.Buckets) { item.Key.Should().NotBeNullOrEmpty(); item.DocCount.Should().BeGreaterOrEqualTo(1); } states.Meta.Should().NotBeNull().And.HaveCount(1); states.Meta["foo"].Should().Be("bar");
Filtering with a regular expression pattern
editUsing terms aggregation with filtering to include values using a regular expression pattern
Fluent DSL example
edits => s .Size(0) .Aggregations(a => a .Terms("states", st => st .Field(p => p.State.Suffix("keyword")) .MinimumDocumentCount(2) .Size(5) .ShardSize(100) .ExecutionHint(TermsAggregationExecutionHint.Map) .Missing("n/a") .Include("(Stable|VeryActive)") .Order(TermsOrder.TermAscending) .Order(TermsOrder.CountDescending) .Meta(m => m .Add("foo", "bar") ) ) )
Object Initializer syntax example
editnew SearchRequest<Project> { Size = 0, Aggregations = new TermsAggregation("states") { Field = Field<Project>(p => p.State.Suffix("keyword")), MinimumDocumentCount = 2, Size = 5, ShardSize = 100, ExecutionHint = TermsAggregationExecutionHint.Map, Missing = "n/a", Include = new TermsIncludeExclude { Pattern = "(Stable|VeryActive)" }, Order = new List<TermsOrder> { TermsOrder.TermAscending, TermsOrder.CountDescending }, Meta = new Dictionary<string, object> { { "foo", "bar" } } } }
Example json output.
{ "size": 0, "aggs": { "states": { "meta": { "foo": "bar" }, "terms": { "field": "state.keyword", "min_doc_count": 2, "size": 5, "shard_size": 100, "execution_hint": "map", "missing": "n/a", "include": "(Stable|VeryActive)", "order": [ { "_term": "asc" }, { "_count": "desc" } ] } } } }
Handling Responses
editresponse.ShouldBeValid(); var states = response.Aggs.Terms("states"); states.Should().NotBeNull(); states.DocCountErrorUpperBound.Should().HaveValue(); states.SumOtherDocCount.Should().HaveValue(); states.Buckets.Should().NotBeNull(); states.Buckets.Count.Should().BeGreaterThan(0); foreach (var item in states.Buckets) { item.Key.Should().NotBeNullOrEmpty(); item.DocCount.Should().BeGreaterOrEqualTo(1); } states.Meta.Should().NotBeNull().And.HaveCount(1); states.Meta["foo"].Should().Be("bar");
Filtering with exact values
editUsing terms aggregation with filtering to include only specific values
Fluent DSL example
edits => s .Size(0) .Aggregations(a => a .Terms("states", st => st .Field(p => p.State.Suffix("keyword")) .MinimumDocumentCount(2) .Size(5) .ShardSize(100) .ExecutionHint(TermsAggregationExecutionHint.Map) .Missing("n/a") .Include(new[] { StateOfBeing.Stable.ToString(), StateOfBeing.VeryActive.ToString() }) .Order(TermsOrder.TermAscending) .Order(TermsOrder.CountDescending) .Meta(m => m .Add("foo", "bar") ) ) )
Object Initializer syntax example
editnew SearchRequest<Project> { Size = 0, Aggregations = new TermsAggregation("states") { Field = Field<Project>(p => p.State.Suffix("keyword")), MinimumDocumentCount = 2, Size = 5, ShardSize = 100, ExecutionHint = TermsAggregationExecutionHint.Map, Missing = "n/a", Include = new TermsIncludeExclude { Values = new[] { StateOfBeing.Stable.ToString(), StateOfBeing.VeryActive.ToString() } }, Order = new List<TermsOrder> { TermsOrder.TermAscending, TermsOrder.CountDescending }, Meta = new Dictionary<string, object> { { "foo", "bar" } } } }
Example json output.
{ "size": 0, "aggs": { "states": { "meta": { "foo": "bar" }, "terms": { "field": "state.keyword", "min_doc_count": 2, "size": 5, "shard_size": 100, "execution_hint": "map", "missing": "n/a", "include": [ "Stable", "VeryActive" ], "order": [ { "_term": "asc" }, { "_count": "desc" } ] } } } }
Handling Responses
editresponse.ShouldBeValid(); var states = response.Aggs.Terms("states"); states.Should().NotBeNull(); states.DocCountErrorUpperBound.Should().HaveValue(); states.SumOtherDocCount.Should().HaveValue(); states.Buckets.Should().NotBeNull(); states.Buckets.Count.Should().BeGreaterThan(0); foreach (var item in states.Buckets) { item.Key.Should().NotBeNullOrEmpty(); item.DocCount.Should().BeGreaterOrEqualTo(1); } states.Meta.Should().NotBeNull().And.HaveCount(1); states.Meta["foo"].Should().Be("bar");
Filtering with partitions
editA terms aggregation that uses partitioning to filter the terms that are returned in the response. Further terms
can be returned by issuing additional requests with an incrementing partition
number.
Partitioning is available only in Elasticsearch 5.2.0+
Fluent DSL example
edits => s .Size(0) .Aggregations(a => a .Terms("commits", st => st .Field(p => p.NumberOfCommits) .Include(0, 10) .Size(5) ) )
Object Initializer syntax example
editnew SearchRequest<Project> { Size = 0, Aggregations = new TermsAggregation("commits") { Field = Field<Project>(p => p.NumberOfCommits), Include = new TermsIncludeExclude { Partition = 0, NumberOfPartitions = 10 }, Size = 5 } }
Example json output.
{ "size": 0, "aggs": { "commits": { "terms": { "field": "numberOfCommits", "size": 5, "include": { "partition": 0, "num_partitions": 10 } } } } }
Handling Responses
editresponse.ShouldBeValid(); var commits = response.Aggs.Terms<int>("commits"); commits.Should().NotBeNull(); commits.DocCountErrorUpperBound.Should().HaveValue(); commits.SumOtherDocCount.Should().HaveValue(); commits.Buckets.Should().NotBeNull(); commits.Buckets.Count.Should().BeGreaterThan(0); foreach (var item in commits.Buckets) { item.Key.Should().BeGreaterThan(0); item.DocCount.Should().BeGreaterOrEqualTo(1); }
Numeric fields
editA terms aggregation on a numeric field
Fluent DSL example
edits => s .Size(0) .Aggregations(a => a .Terms("commits", st => st .Field(p => p.NumberOfCommits) .ShowTermDocCountError() ) )
Object Initializer syntax example
editnew SearchRequest<Project> { Size = 0, Aggregations = new TermsAggregation("commits") { Field = Field<Project>(p => p.NumberOfCommits), ShowTermDocCountError = true } }
Example json output.
{ "size": 0, "aggs": { "commits": { "terms": { "field": "numberOfCommits", "show_term_doc_count_error": true } } } }
Handling Responses
editresponse.ShouldBeValid(); var commits = response.Aggs.Terms<int>("commits"); commits.Should().NotBeNull(); commits.DocCountErrorUpperBound.Should().HaveValue(); commits.SumOtherDocCount.Should().HaveValue(); commits.Buckets.Should().NotBeNull(); commits.Buckets.Count.Should().BeGreaterThan(0); foreach (var item in commits.Buckets) { item.Key.Should().BeGreaterThan(0); item.DocCount.Should().BeGreaterOrEqualTo(1); } commits.Buckets.Should().Contain(b => b.DocCountErrorUpperBound.HasValue);
On this page
- Fluent DSL example
- Object Initializer syntax example
- Handling Responses
- Filtering with a regular expression pattern
- Fluent DSL example
- Object Initializer syntax example
- Handling Responses
- Filtering with exact values
- Fluent DSL example
- Object Initializer syntax example
- Handling Responses
- Filtering with partitions
- Fluent DSL example
- Object Initializer syntax example
- Handling Responses
- Numeric fields
- Fluent DSL example
- Object Initializer syntax example
- Handling Responses