Search in sources :

Example 1 with ValueCount

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.ValueCount in project graylog2-server by Graylog2.

the class SearchesAdapterES7 method fieldStats.

@Override
public FieldStatsResult fieldStats(String query, String filter, TimeRange range, Set<String> indices, String field, boolean includeCardinality, boolean includeStats, boolean includeCount) {
    final SearchesConfig config = SearchesConfig.builder().query(query).filter(filter).range(range).offset(0).limit(-1).build();
    final SearchSourceBuilder searchSourceBuilder = searchRequestFactory.create(config);
    if (includeCount) {
        searchSourceBuilder.aggregation(AggregationBuilders.count(AGG_VALUE_COUNT).field(field));
    }
    if (includeStats) {
        searchSourceBuilder.aggregation(AggregationBuilders.extendedStats(AGG_EXTENDED_STATS).field(field));
    }
    if (includeCardinality) {
        searchSourceBuilder.aggregation(AggregationBuilders.cardinality(AGG_CARDINALITY).field(field));
    }
    if (indices.isEmpty()) {
        return FieldStatsResult.empty(query, searchSourceBuilder.toString());
    }
    final SearchRequest searchRequest = new SearchRequest(indices.toArray(new String[0])).source(searchSourceBuilder);
    final SearchResponse searchResult = client.search(searchRequest, "Unable to retrieve fields stats");
    final List<ResultMessage> resultMessages = extractResultMessages(searchResult);
    final long tookMs = searchResult.getTook().getMillis();
    final ExtendedStats extendedStatsAggregation = searchResult.getAggregations().get(AGG_EXTENDED_STATS);
    final ValueCount valueCountAggregation = searchResult.getAggregations().get(AGG_VALUE_COUNT);
    final Cardinality cardinalityAggregation = searchResult.getAggregations().get(AGG_CARDINALITY);
    return createFieldStatsResult(extendedStatsAggregation, valueCountAggregation, cardinalityAggregation, resultMessages, query, searchSourceBuilder.toString(), tookMs);
}
Also used : SearchRequest(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest) ValueCount(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.ValueCount) SearchesConfig(org.graylog2.indexer.searches.SearchesConfig) Cardinality(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.Cardinality) ExtendedStats(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.ExtendedStats) ResultMessage(org.graylog2.indexer.results.ResultMessage) SearchSourceBuilder(org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchRequest (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchRequest)1 SearchResponse (org.graylog.shaded.elasticsearch7.org.elasticsearch.action.search.SearchResponse)1 Cardinality (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.Cardinality)1 ExtendedStats (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.ExtendedStats)1 ValueCount (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.aggregations.metrics.ValueCount)1 SearchSourceBuilder (org.graylog.shaded.elasticsearch7.org.elasticsearch.search.builder.SearchSourceBuilder)1 ResultMessage (org.graylog2.indexer.results.ResultMessage)1 SearchesConfig (org.graylog2.indexer.searches.SearchesConfig)1