IMPORTANT: No additional bug fixes or documentation updates
will be released for this version. For the latest information, see the
current release documentation.
Filter Aggregation Usage
edit
IMPORTANT: This documentation is no longer updated. Refer to Elastic's version policy and the latest documentation.
Filter Aggregation Usage
editDefines a single bucket of all the documents in the current document set context that match a specified filter. Often this will be used to narrow down the current aggregation context to a specific set of documents.
Be sure to read the Elasticsearch documentation on Filter Aggregation
Fluent DSL example
edita => a
.Filter("bethels_projects", date => date
.Filter(q => q.Term(p => p.LeadDeveloper.FirstName, FirstNameToFind))
.Aggregations(childAggs => childAggs
.Terms("project_tags", avg => avg.Field(p => p.CuratedTags.First().Name.Suffix("keyword")))
)
)
Object Initializer syntax example
editnew FilterAggregation("bethels_projects")
{
Filter = new TermQuery { Field = Field<Project>(p => p.LeadDeveloper.FirstName), Value = FirstNameToFind },
Aggregations =
new TermsAggregation("project_tags") { Field = Field<Project>(p => p.CuratedTags.First().Name.Suffix("keyword")) }
}
Example json output.
{
"bethels_projects": {
"filter": {
"term": {
"leadDeveloper.firstName": {
"value": "pierce"
}
}
},
"aggs": {
"project_tags": {
"terms": {
"field": "curatedTags.name.keyword"
}
}
}
}
}
Handling Responses
editThe AggregateDictionary found on `.Aggregations on SearchResponse<T> has several helper methods
so we can fetch our aggregation results easily in the correct type.
Be sure to read more about these helper methods
response.ShouldBeValid();
var filterAgg = response.Aggregations.Filter("bethels_projects");
filterAgg.Should().NotBeNull();
filterAgg.DocCount.Should().BeGreaterThan(0);
var tags = filterAgg.Terms("project_tags");
tags.Should().NotBeNull();
tags.Buckets.Should().NotBeEmpty();
Empty Filter
editWhen the collection of filters is empty or all are conditionless, NEST will serialize them to an empty object.
Fluent DSL example
edita => a
.Filter("empty_filter", date => date
.Filter(f => f
.Bool(b => b
.Filter(new QueryContainer[0])
)
)
)
Object Initializer syntax example
editnew FilterAggregation("empty_filter")
{
Filter = new BoolQuery
{
Filter = new List<QueryContainer>()
}
}
Example json output.
{
"empty_filter": {
"filter": {}
}
}
Handling Responses
editresponse.ShouldNotBeValid()