Search in sources :

Example 1 with Output

use of org.opensearch.ml.common.parameter.Output in project ml-commons by opensearch-project.

the class AnomalyLocalizerImpl method processBaseEntry.

private void processBaseEntry(AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Result result, AnomalyLocalizationOutput.Bucket bucket, Counter counter, Optional<Map<String, Object>> afterKey, AnomalyLocalizationOutput output, ActionListener<AnomalyLocalizationOutput> listener) {
    SearchRequest request = newSearchRequestForEntry(input, agg, bucket, afterKey);
    client.search(request, wrap(r -> onBaseEntryResponse(r, input, agg, result, bucket, counter, 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)

Example 2 with Output

use of org.opensearch.ml.common.parameter.Output 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)

Example 3 with Output

use of org.opensearch.ml.common.parameter.Output in project ml-commons by opensearch-project.

the class AnomalyLocalizerImpl method getOverallAggregates.

private void getOverallAggregates(AnomalyLocalizationInput input, LocalizationTimeBuckets timeBuckets, AggregationBuilder agg, AnomalyLocalizationOutput output, ActionListener<AnomalyLocalizationOutput> listener) {
    MultiSearchRequest searchRequest = newSearchRequestForOverallAggregates(input, agg, timeBuckets);
    client.multiSearch(searchRequest, wrap(r -> onOverallAggregatesResponse(r, input, agg, output, timeBuckets, 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)

Example 4 with Output

use of org.opensearch.ml.common.parameter.Output in project ml-commons by opensearch-project.

the class MLExecuteTaskRunner method executeTask.

/**
 * Execute algorithm and return result.
 * TODO: 1. support backend task run; 2. support dispatch task to remote node
 * @param request MLExecuteTaskRequest
 * @param transportService transport service
 * @param listener Action listener
 */
@Override
public void executeTask(MLExecuteTaskRequest request, TransportService transportService, ActionListener<MLExecuteTaskResponse> listener) {
    threadPool.executor(TASK_THREAD_POOL).execute(() -> {
        Input input = request.getInput();
        Output output = MLEngine.execute(input);
        MLExecuteTaskResponse response = MLExecuteTaskResponse.builder().output(output).build();
        listener.onResponse(response);
    });
}
Also used : MLExecuteTaskResponse(org.opensearch.ml.common.transport.execute.MLExecuteTaskResponse) Input(org.opensearch.ml.common.parameter.Input) Output(org.opensearch.ml.common.parameter.Output)

Example 5 with Output

use of org.opensearch.ml.common.parameter.Output in project ml-commons by opensearch-project.

the class MachineLearningNodeClient method execute.

@Override
public void execute(Input input, ActionListener<Output> listener) {
    MLExecuteTaskRequest executeTaskRequest = MLExecuteTaskRequest.builder().input(input).build();
    client.execute(MLExecuteTaskAction.INSTANCE, executeTaskRequest, ActionListener.wrap(response -> {
        listener.onResponse(MLExecuteTaskResponse.fromActionResponse(response).getOutput());
    }, listener::onFailure));
}
Also used : MLOutput(org.opensearch.ml.common.parameter.MLOutput) FieldDefaults(lombok.experimental.FieldDefaults) MLModelDeleteRequest(org.opensearch.ml.common.transport.model.MLModelDeleteRequest) RequiredArgsConstructor(lombok.RequiredArgsConstructor) MLInput(org.opensearch.ml.common.parameter.MLInput) MLTrainAndPredictionTaskAction(org.opensearch.ml.common.transport.trainpredict.MLTrainAndPredictionTaskAction) Input(org.opensearch.ml.common.parameter.Input) Function(java.util.function.Function) MLPredictionTaskRequest(org.opensearch.ml.common.transport.prediction.MLPredictionTaskRequest) Output(org.opensearch.ml.common.parameter.Output) AccessLevel(lombok.AccessLevel) MLPredictionTaskAction(org.opensearch.ml.common.transport.prediction.MLPredictionTaskAction) DeleteResponse(org.opensearch.action.delete.DeleteResponse) SearchRequest(org.opensearch.action.search.SearchRequest) ActionListener(org.opensearch.action.ActionListener) ActionResponse(org.opensearch.action.ActionResponse) SearchResponse(org.opensearch.action.search.SearchResponse) MLModel(org.opensearch.ml.common.parameter.MLModel) MLModelGetResponse(org.opensearch.ml.common.transport.model.MLModelGetResponse) MLTaskResponse(org.opensearch.ml.common.transport.MLTaskResponse) MLExecuteTaskAction(org.opensearch.ml.common.transport.execute.MLExecuteTaskAction) MLModelDeleteAction(org.opensearch.ml.common.transport.model.MLModelDeleteAction) NodeClient(org.opensearch.client.node.NodeClient) MLModelGetRequest(org.opensearch.ml.common.transport.model.MLModelGetRequest) MLModelSearchAction(org.opensearch.ml.common.transport.model.MLModelSearchAction) MLExecuteTaskRequest(org.opensearch.ml.common.transport.execute.MLExecuteTaskRequest) MLTrainingTaskAction(org.opensearch.ml.common.transport.training.MLTrainingTaskAction) MLModelGetAction(org.opensearch.ml.common.transport.model.MLModelGetAction) MLExecuteTaskResponse(org.opensearch.ml.common.transport.execute.MLExecuteTaskResponse) MLTrainingTaskRequest(org.opensearch.ml.common.transport.training.MLTrainingTaskRequest) MLExecuteTaskRequest(org.opensearch.ml.common.transport.execute.MLExecuteTaskRequest)

Aggregations

Input (org.opensearch.ml.common.parameter.Input)7 Output (org.opensearch.ml.common.parameter.Output)7 List (java.util.List)5 Data (lombok.Data)5 ActionListener (org.opensearch.action.ActionListener)5 SearchRequest (org.opensearch.action.search.SearchRequest)5 SearchResponse (org.opensearch.action.search.SearchResponse)5 Client (org.opensearch.client.Client)5 Settings (org.opensearch.common.settings.Settings)5 Executable (org.opensearch.ml.engine.Executable)5 SimpleEntry (java.util.AbstractMap.SimpleEntry)4 ArrayList (java.util.ArrayList)4 Arrays (java.util.Arrays)4 Collections (java.util.Collections)4 Map (java.util.Map)4 Optional (java.util.Optional)4 PriorityQueue (java.util.PriorityQueue)4 CountDownLatch (java.util.concurrent.CountDownLatch)4 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4