Search in sources :

Example 11 with KeyedFilter

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

the class FiltersTests method testRewrite.

public void testRewrite() throws IOException {
    // test non-keyed filter that doesn't rewrite
    AggregationBuilder original = new FiltersAggregationBuilder("my-agg", new MatchAllQueryBuilder());
    original.setMetadata(Collections.singletonMap(randomAlphaOfLengthBetween(1, 20), randomAlphaOfLengthBetween(1, 20)));
    AggregationBuilder rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
    assertSame(original, rewritten);
    // test non-keyed filter that does rewrite
    original = new FiltersAggregationBuilder("my-agg", new BoolQueryBuilder());
    rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
    assertNotSame(original, rewritten);
    assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
    assertEquals("my-agg", ((FiltersAggregationBuilder) rewritten).getName());
    assertEquals(1, ((FiltersAggregationBuilder) rewritten).filters().size());
    assertEquals("0", ((FiltersAggregationBuilder) rewritten).filters().get(0).key());
    assertThat(((FiltersAggregationBuilder) rewritten).filters().get(0).filter(), instanceOf(MatchAllQueryBuilder.class));
    assertFalse(((FiltersAggregationBuilder) rewritten).isKeyed());
    // test keyed filter that doesn't rewrite
    original = new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new MatchAllQueryBuilder()));
    rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
    assertSame(original, rewritten);
    // test non-keyed filter that does rewrite
    original = new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new BoolQueryBuilder()));
    rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
    assertNotSame(original, rewritten);
    assertThat(rewritten, instanceOf(FiltersAggregationBuilder.class));
    assertEquals("my-agg", ((FiltersAggregationBuilder) rewritten).getName());
    assertEquals(1, ((FiltersAggregationBuilder) rewritten).filters().size());
    assertEquals("my-filter", ((FiltersAggregationBuilder) rewritten).filters().get(0).key());
    assertThat(((FiltersAggregationBuilder) rewritten).filters().get(0).filter(), instanceOf(MatchAllQueryBuilder.class));
    assertTrue(((FiltersAggregationBuilder) rewritten).isKeyed());
    // test sub-agg filter that does rewrite
    original = new TermsAggregationBuilder("terms").userValueTypeHint(ValueType.BOOLEAN).subAggregation(new FiltersAggregationBuilder("my-agg", new KeyedFilter("my-filter", new BoolQueryBuilder())));
    rewritten = original.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L));
    assertNotSame(original, rewritten);
    assertNotEquals(original, rewritten);
    assertThat(rewritten, instanceOf(TermsAggregationBuilder.class));
    assertThat(rewritten.getSubAggregations().size(), equalTo(1));
    AggregationBuilder subAgg = rewritten.getSubAggregations().iterator().next();
    assertThat(subAgg, instanceOf(FiltersAggregationBuilder.class));
    assertNotSame(original.getSubAggregations().iterator().next(), subAgg);
    assertEquals("my-agg", subAgg.getName());
    assertSame(rewritten, rewritten.rewrite(new QueryRewriteContext(xContentRegistry(), null, null, () -> 0L)));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) TermsAggregationBuilder(org.opensearch.search.aggregations.bucket.terms.TermsAggregationBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) QueryRewriteContext(org.opensearch.index.query.QueryRewriteContext) MatchAllQueryBuilder(org.opensearch.index.query.MatchAllQueryBuilder)

Example 12 with KeyedFilter

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

the class FiltersIT method testWithContextBasedSubAggregation.

public void testWithContextBasedSubAggregation() throws Exception {
    try {
        client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).subAggregation(avg("avg_value"))).get();
        fail("expected execution to fail - an attempt to have a context based numeric sub-aggregation, but there is not value source" + "context which the sub-aggregation can inherit");
    } catch (OpenSearchException e) {
        assertThat(e.getMessage(), is("all shards failed"));
    }
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) OpenSearchException(org.opensearch.OpenSearchException)

Example 13 with KeyedFilter

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

the class FiltersIT method testOtherBucket.

public void testOtherBucket() throws Exception {
    SearchResponse response = client().prepareSearch("idx").addAggregation(filters("tags", randomOrder(new KeyedFilter("tag1", termQuery("tag", "tag1")), new KeyedFilter("tag2", termQuery("tag", "tag2")))).otherBucket(true)).get();
    assertSearchResponse(response);
    Filters filters = response.getAggregations().get("tags");
    assertThat(filters, notNullValue());
    assertThat(filters.getName(), equalTo("tags"));
    assertThat(filters.getBuckets().size(), equalTo(3));
    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));
    bucket = filters.getBucketByKey("_other_");
    assertThat(bucket, Matchers.notNullValue());
    assertThat(bucket.getDocCount(), equalTo((long) numOtherDocs));
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 14 with KeyedFilter

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

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"))))).get();
    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.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) OpenSearchAssertions.assertSearchResponse(org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 15 with KeyedFilter

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

the class FiltersAggregationBuilder method parse.

public static FiltersAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
    List<FiltersAggregator.KeyedFilter> filters = new ArrayList<>();
    XContentParser.Token token;
    String currentFieldName = null;
    String otherBucketKey = null;
    Boolean otherBucket = null;
    boolean keyed = false;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_BOOLEAN) {
            if (OTHER_BUCKET_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                otherBucket = parser.booleanValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if (OTHER_BUCKET_KEY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                otherBucketKey = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (FILTERS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                String key = null;
                while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                    if (token == XContentParser.Token.FIELD_NAME) {
                        key = parser.currentName();
                    } else {
                        QueryBuilder filter = parseInnerQueryBuilder(parser);
                        filters.add(new FiltersAggregator.KeyedFilter(key, filter));
                    }
                }
                keyed = true;
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else if (token == XContentParser.Token.START_ARRAY) {
            if (FILTERS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                List<QueryBuilder> builders = new ArrayList<>();
                while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
                    QueryBuilder filter = parseInnerQueryBuilder(parser);
                    builders.add(filter);
                }
                for (int i = 0; i < builders.size(); i++) {
                    filters.add(new KeyedFilter(String.valueOf(i), builders.get(i)));
                }
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
        }
    }
    if (filters.isEmpty()) {
        throw new IllegalArgumentException("[" + FILTERS_FIELD + "] cannot be empty.");
    }
    FiltersAggregationBuilder factory = new FiltersAggregationBuilder(aggregationName, filters, keyed);
    if (otherBucket == null && otherBucketKey != null) {
        // automatically enable the other bucket if a key is set, as per the doc
        otherBucket = true;
    }
    if (otherBucket != null) {
        factory.otherBucket(otherBucket);
    }
    if (otherBucketKey != null) {
        factory.otherBucketKey(otherBucketKey);
    }
    return factory;
}
Also used : ArrayList(java.util.ArrayList) AbstractQueryBuilder.parseInnerQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

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