Search in sources :

Example 1 with SingleValue

use of org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue in project ml-commons by opensearch-project.

the class AnomalyLocalizerImpl method onNewEntryResponse.

/**
 * Chooses entities from the new bucket that contribute the most to the overall change.
 */
private void onNewEntryResponse(SearchResponse response, AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Result result, AnomalyLocalizationOutput.Bucket outputBucket, PriorityQueue<AnomalyLocalizationOutput.Entity> queue, AnomalyLocalizationOutput output, ActionListener<AnomalyLocalizationOutput> listener) {
    Optional<CompositeAggregation> respAgg = Optional.ofNullable(response.getAggregations()).map(aggs -> (CompositeAggregation) aggs.get(agg.getName()));
    for (CompositeAggregation.Bucket bucket : respAgg.map(a -> a.getBuckets()).orElse(Collections.emptyList())) {
        List<String> key = toStringKey(bucket.getKey(), input);
        AnomalyLocalizationOutput.Entity entity = new AnomalyLocalizationOutput.Entity();
        entity.setKey(key);
        entity.setNewValue(getDoubleValue((SingleValue) bucket.getAggregations().get(agg.getName())));
        entity.setBaseValue(outputBucket.getBase().get().getCounter().get().estimate(key));
        entity.setContributionValue(entity.getNewValue() - entity.getBaseValue());
        if (queue.size() < input.getNumOutputs()) {
            queue.add(entity);
        } else if (queue.comparator().compare(queue.peek(), entity) < 0) {
            queue.poll();
            queue.add(entity);
        }
    }
    Optional<Map<String, Object>> afterKey = respAgg.map(r -> r.afterKey());
    if (afterKey.isPresent()) {
        processNewEntry(input, agg, result, outputBucket, afterKey, queue, output, listener);
    } else {
        List<List<String>> keys = queue.stream().map(AnomalyLocalizationOutput.Entity::getKey).collect(Collectors.toList());
        SearchRequest request = newSearchRequestForEntityKeys(input, agg, outputBucket, keys);
        client.search(request, wrap(r -> onEntityKeysResponse(r, input, agg, result, outputBucket, queue, output, listener), listener::onFailure));
    }
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SneakyThrows(lombok.SneakyThrows) PriorityQueue(java.util.PriorityQueue) NotifyOnceListener(org.opensearch.action.NotifyOnceListener) Executable(org.opensearch.ml.engine.Executable) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) Input(org.opensearch.ml.common.parameter.Input) ArrayList(java.util.ArrayList) CompositeAggregationBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder) Output(org.opensearch.ml.common.parameter.Output) AggregationBuilder(org.opensearch.search.aggregations.AggregationBuilder) LatchedActionListener(org.opensearch.action.LatchedActionListener) KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) Map(java.util.Map) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) SearchResponse(org.opensearch.action.search.SearchResponse) SimpleEntry(java.util.AbstractMap.SimpleEntry) QueryBuilders(org.opensearch.index.query.QueryBuilders) Client(org.opensearch.client.Client) MAX_BUCKET_SETTING(org.opensearch.search.aggregations.MultiBucketConsumerService.MAX_BUCKET_SETTING) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) Filters(org.opensearch.search.aggregations.bucket.filter.Filters) TermQueryBuilder(org.opensearch.index.query.TermQueryBuilder) Settings(org.opensearch.common.settings.Settings) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) Collectors(java.util.stream.Collectors) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) CountDownLatch(java.util.concurrent.CountDownLatch) AggregationBuilders(org.opensearch.search.aggregations.AggregationBuilders) List(java.util.List) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) Data(lombok.Data) Log4j2(lombok.extern.log4j.Log4j2) Optional(java.util.Optional) ActionListener.wrap(org.opensearch.action.ActionListener.wrap) Collections(java.util.Collections) TermsValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) SingleValue(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation.SingleValue) ArrayList(java.util.ArrayList) List(java.util.List) Map(java.util.Map)

Aggregations

SimpleEntry (java.util.AbstractMap.SimpleEntry)1 ArrayList (java.util.ArrayList)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 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 Collectors (java.util.stream.Collectors)1 IntStream (java.util.stream.IntStream)1 Data (lombok.Data)1 SneakyThrows (lombok.SneakyThrows)1 Log4j2 (lombok.extern.log4j.Log4j2)1 ActionListener (org.opensearch.action.ActionListener)1 ActionListener.wrap (org.opensearch.action.ActionListener.wrap)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 NotifyOnceListener (org.opensearch.action.NotifyOnceListener)1