Skip to content

Added Filters aggregation #6974

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 1, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/reference/search/aggregations/bucket.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ include::bucket/global-aggregation.asciidoc[]

include::bucket/filter-aggregation.asciidoc[]

include::bucket/filters-aggregation.asciidoc[]

include::bucket/missing-aggregation.asciidoc[]

include::bucket/nested-aggregation.asciidoc[]
Expand Down
113 changes: 113 additions & 0 deletions docs/reference/search/aggregations/bucket/filters-aggregation.asciidoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
[[search-aggregations-bucket-filters-aggregation]]
=== Filters

Defines a multi bucket aggregations where each bucket is associated with a filter. Each bucket will collect all
documents that match its associated filter.

Example:

[source,js]
--------------------------------------------------
{
"aggs" : {
"messages" : {
"filters" : {
"filters" : {
"errors" : { "query" : { "match" : { "body" : "error" } } },
"warnings" : { "query" : { "match" : { "body" : "warning" } } }
}
},
"aggs" : { "monthly" : { "histogram" : { "field" : "timestamp", "interval" : "1M" } } }
}
}
}
--------------------------------------------------

In the above example, we analyze log messages. The aggregation will build two collection (buckets) of log messages - one
for all those containing an error, and another for all those containing a warning. And for each of these buckets it will
break them down by month.

Response:

[source,js]
--------------------------------------------------
{
...

"aggs" : {
"messages" : {
"buckets" : {
"errors" : {
"doc_count" : 34,
"monthly" : {
"buckets : [
... // the histogram monthly breakdown
]
}
},
"warnings" : {
"doc_count" : 439,
"monthly" : {
"buckets : [
... // the histogram monthly breakdown
]
}
}
}
}
}
}
--------------------------------------------------

==== Anonymous filters

The filters field can also be provided as an array of filters, as in the following request:

[source,js]
--------------------------------------------------
{
"aggs" : {
"messages" : {
"filters" : {
"filters" : [
"query" : { "match" : { "body" : "error" } },
"query" : { "match" : { "body" : "warning" } }
]
},
"aggs" : { "monthly" : { "histogram" : { "field" : "timestamp", "interval" : "1M" } } }
}
}
}
--------------------------------------------------

The filtered buckets are returned in the same order as provided in the request. The response for this example would be:

[source,js]
--------------------------------------------------
{
...

"aggs" : {
"messages" : {
"buckets" : [
{
"doc_count" : 34,
"monthly" : {
"buckets : [
... // the histogram monthly breakdown
]
}
},
{
"doc_count" : 439,
"monthly" : {
"buckets : [
... // the histogram monthly breakdown
]
}
}
]
}
}
}
--------------------------------------------------
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.elasticsearch.search.aggregations;

import org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridBuilder;
import org.elasticsearch.search.aggregations.bucket.global.GlobalBuilder;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramBuilder;
Expand Down Expand Up @@ -85,6 +86,10 @@ public static FilterAggregationBuilder filter(String name) {
return new FilterAggregationBuilder(name);
}

public static FiltersAggregationBuilder filters(String name) {
return new FiltersAggregationBuilder(name);
}

public static GlobalBuilder global(String name) {
return new GlobalBuilder(name);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.common.inject.multibindings.Multibinder;
import org.elasticsearch.search.aggregations.bucket.filter.FilterParser;
import org.elasticsearch.search.aggregations.bucket.filters.FiltersParser;
import org.elasticsearch.search.aggregations.bucket.geogrid.GeoHashGridParser;
import org.elasticsearch.search.aggregations.bucket.global.GlobalParser;
import org.elasticsearch.search.aggregations.bucket.histogram.DateHistogramParser;
Expand Down Expand Up @@ -72,6 +73,7 @@ public AggregationModule() {
parsers.add(GlobalParser.class);
parsers.add(MissingParser.class);
parsers.add(FilterParser.class);
parsers.add(FiltersParser.class);
parsers.add(TermsParser.class);
parsers.add(SignificantTermsParser.class);
parsers.add(RangeParser.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

import org.elasticsearch.common.inject.AbstractModule;
import org.elasticsearch.search.aggregations.bucket.filter.InternalFilter;
import org.elasticsearch.search.aggregations.bucket.filters.InternalFilters;
import org.elasticsearch.search.aggregations.bucket.geogrid.InternalGeoHashGrid;
import org.elasticsearch.search.aggregations.bucket.global.InternalGlobal;
import org.elasticsearch.search.aggregations.bucket.histogram.InternalDateHistogram;
Expand Down Expand Up @@ -74,6 +75,7 @@ protected void configure() {
// buckets
InternalGlobal.registerStreams();
InternalFilter.registerStreams();
InternalFilters.registerStream();
InternalMissing.registerStreams();
StringTerms.registerStreams();
LongTerms.registerStreams();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.aggregations.bucket.filters;

import org.elasticsearch.search.aggregations.bucket.MultiBucketsAggregation;

import java.util.Collection;

/**
* A multi bucket aggregation where the buckets are defined by a set of filters (a bucket per filter). Each bucket
* will collect all documents matching its filter.
*/
public interface Filters extends MultiBucketsAggregation {

/**
* A bucket associated with a specific filter (identified by its key)
*/
public static interface Bucket extends MultiBucketsAggregation.Bucket {
}

Collection<? extends Bucket> getBuckets();

@Override
Bucket getBucketByKey(String key);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.elasticsearch.search.aggregations.bucket.filters;

import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.FilterBuilder;
import org.elasticsearch.search.aggregations.AggregationBuilder;
import org.elasticsearch.search.builder.SearchSourceBuilderException;

import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
*
*/
public class FiltersAggregationBuilder extends AggregationBuilder<FiltersAggregationBuilder> {

private Map<String, FilterBuilder> keyedFilters = null;
private List<FilterBuilder> nonKeyedFilters = null;

public FiltersAggregationBuilder(String name) {
super(name, InternalFilters.TYPE.name());
}

public FiltersAggregationBuilder filter(String key, FilterBuilder filter) {
if (keyedFilters == null) {
keyedFilters = new LinkedHashMap<>();
}
keyedFilters.put(key, filter);
return this;
}

public FiltersAggregationBuilder filter(FilterBuilder filter) {
if (nonKeyedFilters == null) {
nonKeyedFilters = new ArrayList<>();
}
nonKeyedFilters.add(filter);
return this;
}


@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject();
if (keyedFilters == null && nonKeyedFilters == null) {
throw new SearchSourceBuilderException("At least one filter must be set on filter aggregation [" + name + "]");
}
if (keyedFilters != null && nonKeyedFilters != null) {
throw new SearchSourceBuilderException("Cannot add both keyed and non-keyed filters to filters aggregation");
}

if (keyedFilters != null) {
builder.startObject("filters");
for (Map.Entry<String, FilterBuilder> entry : keyedFilters.entrySet()) {
builder.field(entry.getKey());
entry.getValue().toXContent(builder, params);
}
builder.endObject();
}
if (nonKeyedFilters != null) {
builder.startArray("filters");
for (FilterBuilder filterBuilder : nonKeyedFilters) {
filterBuilder.toXContent(builder, params);
}
builder.endArray();

}
return builder.endObject();
}
}
Loading