Search in sources :

Example 6 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.

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())))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, 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.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Histogram(org.opensearch.search.aggregations.bucket.histogram.Histogram) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 7 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.

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.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)

Example 8 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.

the class FiltersAggregationBuilder method doRewrite.

@Override
protected AggregationBuilder doRewrite(QueryRewriteContext queryShardContext) throws IOException {
    List<KeyedFilter> rewrittenFilters = new ArrayList<>(filters.size());
    boolean changed = false;
    for (KeyedFilter kf : filters) {
        QueryBuilder result = Rewriteable.rewrite(kf.filter(), queryShardContext);
        rewrittenFilters.add(new KeyedFilter(kf.key(), result));
        if (result != kf.filter()) {
            changed = true;
        }
    }
    if (changed) {
        FiltersAggregationBuilder rewritten = new FiltersAggregationBuilder(getName(), rewrittenFilters, this.keyed);
        rewritten.otherBucket(otherBucket);
        rewritten.otherBucketKey(otherBucketKey);
        return rewritten;
    } else {
        return this;
    }
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) ArrayList(java.util.ArrayList) AbstractQueryBuilder.parseInnerQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder)

Example 9 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.

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.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)

Example 10 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.

the class FiltersTests method createTestAggregatorBuilder.

@Override
protected FiltersAggregationBuilder createTestAggregatorBuilder() {
    int size = randomIntBetween(1, 20);
    FiltersAggregationBuilder factory;
    if (randomBoolean()) {
        KeyedFilter[] filters = new KeyedFilter[size];
        int i = 0;
        for (String key : randomUnique(() -> randomAlphaOfLengthBetween(1, 20), size)) {
            filters[i++] = new KeyedFilter(key, QueryBuilders.termQuery(randomAlphaOfLengthBetween(5, 20), randomAlphaOfLengthBetween(5, 20)));
        }
        factory = new FiltersAggregationBuilder(randomAlphaOfLengthBetween(1, 20), filters);
    } else {
        QueryBuilder[] filters = new QueryBuilder[size];
        for (int i = 0; i < size; i++) {
            filters[i] = QueryBuilders.termQuery(randomAlphaOfLengthBetween(5, 20), randomAlphaOfLengthBetween(5, 20));
        }
        factory = new FiltersAggregationBuilder(randomAlphaOfLengthBetween(1, 20), filters);
    }
    if (randomBoolean()) {
        factory.otherBucket(randomBoolean());
    }
    if (randomBoolean()) {
        factory.otherBucketKey(randomAlphaOfLengthBetween(1, 20));
    }
    return factory;
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder)

Aggregations

KeyedFilter (org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)17 SearchResponse (org.opensearch.action.search.SearchResponse)9 Filters (org.opensearch.search.aggregations.bucket.filter.Filters)9 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)8 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)4 QueryBuilder (org.opensearch.index.query.QueryBuilder)4 FiltersAggregationBuilder (org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder)4 ArrayList (java.util.ArrayList)3 AbstractQueryBuilder.parseInnerQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 Avg (org.opensearch.search.aggregations.metrics.Avg)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 PriorityQueue (java.util.PriorityQueue)1