Search in sources :

Example 6 with FiltersAggregationBuilder

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder in project sonarqube by SonarSource.

the class ProjectMeasuresIndex method createQualityGateFacet.

private static AbstractAggregationBuilder createQualityGateFacet() {
    FiltersAggregationBuilder qualityGateStatusFilter = AggregationBuilders.filters(ALERT_STATUS_KEY);
    QUALITY_GATE_STATUS.entrySet().forEach(entry -> qualityGateStatusFilter.filter(entry.getKey(), termQuery(FIELD_QUALITY_GATE_STATUS, entry.getValue())));
    return qualityGateStatusFilter;
}
Also used : FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder)

Example 7 with FiltersAggregationBuilder

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

the class FiltersTests method testFiltersSortedByKey.

/**
     * Test that when passing in keyed filters as list or array, the list stored internally is sorted by key
     * Also check the list passed in is not modified by this but rather copied
     */
public void testFiltersSortedByKey() {
    KeyedFilter[] original = new KeyedFilter[] { new KeyedFilter("bbb", new MatchNoneQueryBuilder()), new KeyedFilter("aaa", new MatchNoneQueryBuilder()) };
    FiltersAggregationBuilder builder;
    builder = new FiltersAggregationBuilder("my-agg", original);
    assertEquals("aaa", builder.filters().get(0).key());
    assertEquals("bbb", builder.filters().get(1).key());
    // original should be unchanged
    assertEquals("bbb", original[0].key());
    assertEquals("aaa", original[1].key());
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) MatchNoneQueryBuilder(org.elasticsearch.index.query.MatchNoneQueryBuilder) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder)

Example 8 with FiltersAggregationBuilder

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

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(() -> randomAsciiOfLengthBetween(1, 20), size)) {
            filters[i++] = new KeyedFilter(key, QueryBuilders.termQuery(randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20)));
        }
        factory = new FiltersAggregationBuilder(randomAsciiOfLengthBetween(1, 20), filters);
    } else {
        QueryBuilder[] filters = new QueryBuilder[size];
        for (int i = 0; i < size; i++) {
            filters[i] = QueryBuilders.termQuery(randomAsciiOfLengthBetween(5, 20), randomAsciiOfLengthBetween(5, 20));
        }
        factory = new FiltersAggregationBuilder(randomAsciiOfLengthBetween(1, 20), filters);
    }
    if (randomBoolean()) {
        factory.otherBucket(randomBoolean());
    }
    if (randomBoolean()) {
        factory.otherBucketKey(randomAsciiOfLengthBetween(1, 20));
    }
    return factory;
}
Also used : KeyedFilter(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MatchNoneQueryBuilder(org.elasticsearch.index.query.MatchNoneQueryBuilder)

Example 9 with FiltersAggregationBuilder

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

the class FiltersAggregatorTests method testEmpty.

public void testEmpty() throws Exception {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader, true, true);
    int numFilters = randomIntBetween(1, 10);
    QueryBuilder[] filters = new QueryBuilder[numFilters];
    for (int i = 0; i < filters.length; i++) {
        filters[i] = QueryBuilders.termQuery("field", randomAsciiOfLength(5));
    }
    FiltersAggregationBuilder builder = new FiltersAggregationBuilder("test", filters);
    builder.otherBucketKey("other");
    InternalFilters response = search(indexSearcher, new MatchAllDocsQuery(), builder, fieldType);
    assertEquals(response.getBuckets().size(), numFilters);
    for (InternalFilters.InternalBucket filter : response.getBuckets()) {
        assertEquals(filter.getDocCount(), 0);
    }
    indexReader.close();
    directory.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) IndexReader(org.apache.lucene.index.IndexReader) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 10 with FiltersAggregationBuilder

use of org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder in project sonarqube by SonarSource.

the class ComponentIndex method createAggregation.

private static FiltersAggregationBuilder createAggregation(ComponentIndexQuery query) {
    FiltersAggregationBuilder filtersAggregation = AggregationBuilders.filters(FILTERS_AGGREGATION_NAME).subAggregation(createSubAggregation(query));
    query.getQualifiers().forEach(q -> filtersAggregation.filter(q, termQuery(FIELD_QUALIFIER, q)));
    return filtersAggregation;
}
Also used : FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder)

Aggregations

FiltersAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder)10 InternalFilters (org.elasticsearch.search.aggregations.bucket.filters.InternalFilters)4 IndexReader (org.apache.lucene.index.IndexReader)3 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)3 IndexSearcher (org.apache.lucene.search.IndexSearcher)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 Directory (org.apache.lucene.store.Directory)3 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)3 Document (org.apache.lucene.document.Document)2 Field (org.apache.lucene.document.Field)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 MatchNoneQueryBuilder (org.elasticsearch.index.query.MatchNoneQueryBuilder)2 FiltersAggregator (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator)2 KeyedFilter (org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator.KeyedFilter)2 HashSet (java.util.HashSet)1 SearchResponse (org.elasticsearch.action.search.SearchResponse)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 QueryParseContext (org.elasticsearch.index.query.QueryParseContext)1