Search in sources :

Example 1 with KeyedFilter

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.

the class FiltersAggregationBuilder method internalXContent.

@Override
protected XContentBuilder internalXContent(XContentBuilder builder, Params params) throws IOException {
    builder.startObject();
    if (keyed) {
        builder.startObject(FiltersAggregator.FILTERS_FIELD.getPreferredName());
        for (KeyedFilter keyedFilter : filters) {
            builder.field(keyedFilter.key(), keyedFilter.filter());
        }
        builder.endObject();
    } else {
        builder.startArray(FiltersAggregator.FILTERS_FIELD.getPreferredName());
        for (KeyedFilter keyedFilter : filters) {
            builder.value(keyedFilter.filter());
        }
        builder.endArray();
    }
    builder.field(FiltersAggregator.OTHER_BUCKET_FIELD.getPreferredName(), otherBucket);
    builder.field(FiltersAggregator.OTHER_BUCKET_KEY_FIELD.getPreferredName(), otherBucketKey);
    builder.endObject();
    return builder;
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter)

Example 2 with KeyedFilter

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.

the class FiltersAggregationBuilder method doWriteTo.

@Override
protected void doWriteTo(StreamOutput out) throws IOException {
    out.writeBoolean(keyed);
    out.writeVInt(filters.size());
    if (keyed) {
        for (KeyedFilter keyedFilter : filters) {
            keyedFilter.writeTo(out);
        }
    } else {
        for (KeyedFilter keyedFilter : filters) {
            out.writeNamedWriteable(keyedFilter.filter());
        }
    }
    out.writeBoolean(otherBucket);
    out.writeString(otherBucketKey);
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter)

Example 3 with KeyedFilter

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.

the class FiltersIT method testEmptyAggregation.

public void testEmptyAggregation() throws Exception {
    SearchResponse searchResponse = client().prepareSearch("empty_bucket_idx").setQuery(matchAllQuery()).addAggregation(histogram("histo").field("value").interval(1L).minDocCount(0).subAggregation(filters("filters", new KeyedFilter("all", matchAllQuery())))).execute().actionGet();
    assertThat(searchResponse.getHits().getTotalHits(), equalTo(2L));
    Histogram histo = searchResponse.getAggregations().get("histo");
    assertThat(histo, Matchers.notNullValue());
    Histogram.Bucket bucket = histo.getBuckets().get(1);
    assertThat(bucket, Matchers.notNullValue());
    Filters filters = bucket.getAggregations().get("filters");
    assertThat(filters, notNullValue());
    Filters.Bucket all = filters.getBucketByKey("all");
    assertThat(all, Matchers.notNullValue());
    assertThat(all.getKeyAsString(), equalTo("all"));
    assertThat(all.getDocCount(), is(0L));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Histogram(org.elasticsearch.search.aggregations.bucket.histogram.Histogram) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 4 with KeyedFilter

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.

the class FiltersIT method testSimple.

public void testSimple() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2"))))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(2));
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
    bucket = filters.getBucketByKey("tag2");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag2Docs));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Example 5 with KeyedFilter

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter in project elasticsearch by elastic.

the class FiltersIT method testEmptyFilterDeclarations.

// See NullPointer issue when filters are empty:
// https://github.com/elastic/elasticsearch/issues/8438
public void testEmptyFilterDeclarations() throws Exception {
    QueryBuilder emptyFilter = new BoolQueryBuilder();
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("all", emptyFilter), new KeyedFilter("tag1", termQuery("tag", "tag1"))))).execute().actionGet();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    Filters.Bucket allBucket = filters.getBucketByKey("all");
    assertThat(allBucket.getDocCount(), equalTo((long) numDocs));
    Filters.Bucket bucket = filters.getBucketByKey("tag1");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numTag1Docs));
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) Filters(org.elasticsearch.search.aggregations.bucket.filters.Filters) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

KeyedFilter (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter)13 SearchResponse (org.elasticsearch.action.search.SearchResponse)8 Filters (org.elasticsearch.search.aggregations.bucket.filters.Filters)8 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)8 MatchNoneQueryBuilder (org.elasticsearch.index.query.MatchNoneQueryBuilder)2 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)2 FiltersAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder)2 Histogram (org.elasticsearch.search.aggregations.bucket.histogram.Histogram)2 Avg (org.elasticsearch.search.aggregations.metrics.avg.Avg)2 FacetValue (io.vertigo.dynamo.collections.model.FacetValue)1 DataType (io.vertigo.dynamo.domain.metamodel.DataType)1 ArrayList (java.util.ArrayList)1 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)1