Search in sources :

Example 76 with Indices

use of org.graylog2.indexer.indices.Indices 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)

Example 77 with Indices

use of org.graylog2.indexer.indices.Indices in project graylog2-server by Graylog2.

the class MoreSearch method getAffectedIndices.

private Set<String> getAffectedIndices(Set<String> streamIds, TimeRange timeRange) {
    final SortedSet<IndexRange> indexRanges = indexRangeService.find(timeRange.getFrom(), timeRange.getTo());
    // We support an empty streams list and return all affected indices in that case.
    if (streamIds.isEmpty()) {
        return indexRanges.stream().map(IndexRange::indexName).collect(Collectors.toSet());
    } else {
        final Set<Stream> streams = loadStreams(streamIds);
        final IndexRangeContainsOneOfStreams indexRangeContainsOneOfStreams = new IndexRangeContainsOneOfStreams(streams);
        return indexRanges.stream().filter(indexRangeContainsOneOfStreams).map(IndexRange::indexName).collect(Collectors.toSet());
    }
}
Also used : IndexRange(org.graylog2.indexer.ranges.IndexRange) Stream(org.graylog2.plugin.streams.Stream) IndexRangeContainsOneOfStreams(org.graylog.plugins.views.search.IndexRangeContainsOneOfStreams)

Aggregations

IndexSet (org.graylog2.indexer.IndexSet)20 IndexRange (org.graylog2.indexer.ranges.IndexRange)19 Set (java.util.Set)18 Test (org.junit.Test)18 DateTime (org.joda.time.DateTime)17 Map (java.util.Map)16 Collectors (java.util.stream.Collectors)15 List (java.util.List)14 TimeRange (org.graylog2.plugin.indexer.searches.timeranges.TimeRange)14 ZonedDateTime (java.time.ZonedDateTime)13 Inject (javax.inject.Inject)13 JsonNode (com.fasterxml.jackson.databind.JsonNode)11 ImmutableMap (com.google.common.collect.ImmutableMap)11 HashMap (java.util.HashMap)11 Indices (org.graylog2.indexer.indices.Indices)11 MongoIndexRange (org.graylog2.indexer.ranges.MongoIndexRange)11 Logger (org.slf4j.Logger)11 LoggerFactory (org.slf4j.LoggerFactory)11 Optional (java.util.Optional)10 ImmutableSet (com.google.common.collect.ImmutableSet)9