Search in sources :

Example 6 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project graylog2-server by Graylog2.

the class Searches method termsStats.

public TermsStatsResult termsStats(String keyField, String valueField, TermsStatsOrder order, int size, String query, String filter, TimeRange range) {
    if (size == 0) {
        size = 50;
    }
    SearchRequestBuilder srb;
    if (filter == null) {
        srb = standardSearchRequest(query, determineAffectedIndices(range, filter), range);
    } else {
        srb = filteredSearchRequest(query, filter, determineAffectedIndices(range, filter), range);
    }
    Terms.Order termsOrder;
    switch(order) {
        case COUNT:
            termsOrder = Terms.Order.count(true);
            break;
        case REVERSE_COUNT:
            termsOrder = Terms.Order.count(false);
            break;
        case TERM:
            termsOrder = Terms.Order.term(true);
            break;
        case REVERSE_TERM:
            termsOrder = Terms.Order.term(false);
            break;
        case MIN:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "min", true);
            break;
        case REVERSE_MIN:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "min", false);
            break;
        case MAX:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "max", true);
            break;
        case REVERSE_MAX:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "max", false);
            break;
        case MEAN:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "avg", true);
            break;
        case REVERSE_MEAN:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "avg", false);
            break;
        case TOTAL:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "sum", true);
            break;
        case REVERSE_TOTAL:
            termsOrder = Terms.Order.aggregation(AGG_STATS, "sum", false);
            break;
        default:
            termsOrder = Terms.Order.count(true);
    }
    FilterAggregationBuilder builder = AggregationBuilders.filter(AGG_FILTER).subAggregation(AggregationBuilders.terms(AGG_TERMS_STATS).field(keyField).subAggregation(AggregationBuilders.stats(AGG_STATS).field(valueField)).order(termsOrder).size(size)).filter(standardAggregationFilters(range, filter));
    srb.addAggregation(builder);
    final SearchRequest request = srb.request();
    SearchResponse r = c.search(request).actionGet();
    recordEsMetrics(r, range);
    final Filter f = r.getAggregations().get(AGG_FILTER);
    return new TermsStatsResult(f.getAggregations().get(AGG_TERMS_STATS), query, request.source(), r.getTook());
}
Also used : SearchRequest(org.elasticsearch.action.search.SearchRequest) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) Filter(org.elasticsearch.search.aggregations.bucket.filter.Filter) Terms(org.elasticsearch.search.aggregations.bucket.terms.Terms) TermsStatsResult(org.graylog2.indexer.results.TermsStatsResult) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 7 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project fess by codelibs.

the class BsDataConfigToRoleCA method filter.

// ===================================================================================
//                                                                     Aggregation Set
//                                                                           =========
public void filter(String name, EsAbstractConditionQuery.OperatorCall<BsDataConfigToRoleCQ> queryLambda, ConditionOptionCall<FilterAggregationBuilder> opLambda, OperatorCall<BsDataConfigToRoleCA> aggsLambda) {
    DataConfigToRoleCQ cq = new DataConfigToRoleCQ();
    if (queryLambda != null) {
        queryLambda.callback(cq);
    }
    FilterAggregationBuilder builder = regFilterA(name, cq.getQuery());
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        DataConfigToRoleCA ca = new DataConfigToRoleCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BsDataConfigToRoleCQ(org.codelibs.fess.es.config.cbean.cq.bs.BsDataConfigToRoleCQ) DataConfigToRoleCQ(org.codelibs.fess.es.config.cbean.cq.DataConfigToRoleCQ) DataConfigToRoleCA(org.codelibs.fess.es.config.cbean.ca.DataConfigToRoleCA)

Example 8 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project fess by codelibs.

the class BsWebConfigToLabelCA method filter.

// ===================================================================================
//                                                                     Aggregation Set
//                                                                           =========
public void filter(String name, EsAbstractConditionQuery.OperatorCall<BsWebConfigToLabelCQ> queryLambda, ConditionOptionCall<FilterAggregationBuilder> opLambda, OperatorCall<BsWebConfigToLabelCA> aggsLambda) {
    WebConfigToLabelCQ cq = new WebConfigToLabelCQ();
    if (queryLambda != null) {
        queryLambda.callback(cq);
    }
    FilterAggregationBuilder builder = regFilterA(name, cq.getQuery());
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        WebConfigToLabelCA ca = new WebConfigToLabelCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
Also used : BsWebConfigToLabelCQ(org.codelibs.fess.es.config.cbean.cq.bs.BsWebConfigToLabelCQ) WebConfigToLabelCQ(org.codelibs.fess.es.config.cbean.cq.WebConfigToLabelCQ) WebConfigToLabelCA(org.codelibs.fess.es.config.cbean.ca.WebConfigToLabelCA) FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)

Example 9 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project fess by codelibs.

the class BsSearchFieldLogCA method filter.

// ===================================================================================
//                                                                     Aggregation Set
//                                                                           =========
public void filter(String name, EsAbstractConditionQuery.OperatorCall<BsSearchFieldLogCQ> queryLambda, ConditionOptionCall<FilterAggregationBuilder> opLambda, OperatorCall<BsSearchFieldLogCA> aggsLambda) {
    SearchFieldLogCQ cq = new SearchFieldLogCQ();
    if (queryLambda != null) {
        queryLambda.callback(cq);
    }
    FilterAggregationBuilder builder = regFilterA(name, cq.getQuery());
    if (opLambda != null) {
        opLambda.callback(builder);
    }
    if (aggsLambda != null) {
        SearchFieldLogCA ca = new SearchFieldLogCA();
        aggsLambda.callback(ca);
        ca.getAggregationBuilderList().forEach(builder::subAggregation);
    }
}
Also used : FilterAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) SearchFieldLogCQ(org.codelibs.fess.es.log.cbean.cq.SearchFieldLogCQ) BsSearchFieldLogCQ(org.codelibs.fess.es.log.cbean.cq.bs.BsSearchFieldLogCQ) SearchFieldLogCA(org.codelibs.fess.es.log.cbean.ca.SearchFieldLogCA)

Example 10 with FilterAggregationBuilder

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder in project graylog2-server by Graylog2.

the class IndexToolsAdapterES6 method fieldHistogram.

@Override
public Map<DateTime, Map<String, Long>> fieldHistogram(String fieldName, Set<String> indices, Optional<Set<String>> includedStreams, long interval) {
    final BoolQueryBuilder queryBuilder = buildStreamIdFilter(includedStreams);
    final FilterAggregationBuilder the_filter = AggregationBuilders.filter(AGG_FILTER, queryBuilder).subAggregation(AggregationBuilders.dateHistogram(AGG_DATE_HISTOGRAM).field("timestamp").subAggregation(AggregationBuilders.terms(AGG_MESSAGE_FIELD).field(fieldName)).interval(interval).minDocCount(1L));
    final SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).aggregation(the_filter);
    final Search.Builder searchBuilder = new Search.Builder(searchSourceBuilder.toString()).addIndex(indices).addType(IndexMapping.TYPE_MESSAGE);
    final SearchResult searchResult = JestUtils.execute(this.jestClient, searchBuilder.build(), () -> "Unable to retrieve field histogram.");
    final FilterAggregation filterAggregation = searchResult.getAggregations().getFilterAggregation(AGG_FILTER);
    final DateHistogramAggregation dateHistogram = filterAggregation.getDateHistogramAggregation(AGG_DATE_HISTOGRAM);
    final List<DateHistogramAggregation.DateHistogram> histogramBuckets = dateHistogram.getBuckets();
    final Map<DateTime, Map<String, Long>> result = Maps.newHashMapWithExpectedSize(histogramBuckets.size());
    for (HistogramAggregation.Histogram bucket : histogramBuckets) {
        final DateTime date = new DateTime(bucket.getKey()).toDateTime(DateTimeZone.UTC);
        final TermsAggregation sourceFieldAgg = bucket.getTermsAggregation(AGG_MESSAGE_FIELD);
        final List<TermsAggregation.Entry> termBuckets = sourceFieldAgg.getBuckets();
        final HashMap<String, Long> termCounts = Maps.newHashMapWithExpectedSize(termBuckets.size());
        for (TermsAggregation.Entry termBucket : termBuckets) {
            termCounts.put(termBucket.getKeyAsString(), termBucket.getCount());
        }
        result.put(date, termCounts);
    }
    return ImmutableMap.copyOf(result);
}
Also used : TermsAggregation(io.searchbox.core.search.aggregation.TermsAggregation) FilterAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) FilterAggregationBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder) BoolQueryBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) SearchResult(io.searchbox.core.SearchResult) DateTime(org.joda.time.DateTime) SearchSourceBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.search.builder.SearchSourceBuilder) HistogramAggregation(io.searchbox.core.search.aggregation.HistogramAggregation) DateHistogramAggregation(io.searchbox.core.search.aggregation.DateHistogramAggregation) BoolQueryBuilder(org.graylog.shaded.elasticsearch6.org.elasticsearch.index.query.BoolQueryBuilder) Search(io.searchbox.core.Search) FilterAggregation(io.searchbox.core.search.aggregation.FilterAggregation) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DateHistogramAggregation(io.searchbox.core.search.aggregation.DateHistogramAggregation)

Aggregations

FilterAggregationBuilder (org.elasticsearch.search.aggregations.bucket.filter.FilterAggregationBuilder)33 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)15 TermsAggregationBuilder (org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder)8 Map (java.util.Map)6 SearchRequest (org.elasticsearch.action.search.SearchRequest)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)6 Test (org.junit.Test)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 AggregationBuilder (org.elasticsearch.search.aggregations.AggregationBuilder)5 Filter (org.elasticsearch.search.aggregations.bucket.filter.Filter)5 Terms (org.elasticsearch.search.aggregations.bucket.terms.Terms)5 DateTime (org.joda.time.DateTime)5 Search (io.searchbox.core.Search)3 Arrays (java.util.Arrays)3 List (java.util.List)3 Objects (java.util.Objects)3 Collectors (java.util.stream.Collectors)3 Nullable (javax.annotation.Nullable)3 ImmutableMap (com.google.common.collect.ImmutableMap)2