Search in sources :

Example 6 with Aggregation

use of org.opensearch.search.aggregations.Aggregation in project anomaly-detection by opensearch-project.

the class SearchFeatureDaoTests method setup.

@Before
public void setup() throws Exception {
    MockitoAnnotations.initMocks(this);
    PowerMockito.mockStatic(ParseUtils.class);
    interpolator = new LinearUniformInterpolator(new SingleFeatureLinearUniformInterpolator());
    ExecutorService executorService = mock(ExecutorService.class);
    when(threadPool.executor(AnomalyDetectorPlugin.AD_THREAD_POOL_NAME)).thenReturn(executorService);
    doAnswer(invocation -> {
        Runnable runnable = invocation.getArgument(0);
        runnable.run();
        return null;
    }).when(executorService).execute(any(Runnable.class));
    settings = Settings.EMPTY;
    ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.unmodifiableSet(new HashSet<>(Arrays.asList(AnomalyDetectorSettings.MAX_ENTITIES_FOR_PREVIEW, AnomalyDetectorSettings.PAGE_SIZE))));
    when(clusterService.getClusterSettings()).thenReturn(clusterSettings);
    searchFeatureDao = spy(new SearchFeatureDao(client, xContent, interpolator, clientUtil, settings, clusterService, AnomalyDetectorSettings.NUM_SAMPLES_PER_TREE));
    detectionInterval = new IntervalTimeConfiguration(1, ChronoUnit.MINUTES);
    detectorId = "123";
    when(detector.getDetectorId()).thenReturn(detectorId);
    when(detector.getTimeField()).thenReturn("testTimeField");
    when(detector.getIndices()).thenReturn(Arrays.asList("testIndices"));
    when(detector.getDetectionInterval()).thenReturn(detectionInterval);
    when(detector.getFilterQuery()).thenReturn(QueryBuilders.matchAllQuery());
    when(detector.getCategoryField()).thenReturn(Collections.singletonList("a"));
    searchSourceBuilder = SearchSourceBuilder.fromXContent(XContentType.JSON.xContent().createParser(xContent, LoggingDeprecationHandler.INSTANCE, "{}"));
    searchRequest = new SearchRequest(detector.getIndices().toArray(new String[0]));
    aggsMap = new HashMap<>();
    when(max.getName()).thenReturn(CommonName.AGG_NAME_MAX_TIME);
    List<Aggregation> list = new ArrayList<>();
    list.add(max);
    Aggregations aggregations = new Aggregations(list);
    SearchHits hits = new SearchHits(new SearchHit[0], new TotalHits(1L, TotalHits.Relation.EQUAL_TO), 1f);
    when(searchResponse.getHits()).thenReturn(hits);
    doReturn(Optional.of(searchResponse)).when(clientUtil).timedRequest(eq(searchRequest), anyObject(), Matchers.<BiConsumer<SearchRequest, ActionListener<SearchResponse>>>anyObject());
    when(searchResponse.getAggregations()).thenReturn(aggregations);
    doReturn(Optional.of(searchResponse)).when(clientUtil).throttledTimedRequest(eq(searchRequest), anyObject(), Matchers.<BiConsumer<SearchRequest, ActionListener<SearchResponse>>>anyObject(), anyObject());
    multiSearchRequest = new MultiSearchRequest();
    SearchRequest request = new SearchRequest(detector.getIndices().toArray(new String[0]));
    multiSearchRequest.add(request);
    doReturn(Optional.of(multiSearchResponse)).when(clientUtil).timedRequest(eq(multiSearchRequest), anyObject(), Matchers.<BiConsumer<MultiSearchRequest, ActionListener<MultiSearchResponse>>>anyObject());
    when(multiSearchResponse.getResponses()).thenReturn(new Item[] { multiSearchResponseItem });
    when(multiSearchResponseItem.getResponse()).thenReturn(searchResponse);
    gson = PowerMockito.mock(Gson.class);
}
Also used : TotalHits(org.apache.lucene.search.TotalHits) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) ClusterSettings(org.opensearch.common.settings.ClusterSettings) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) Aggregations(org.opensearch.search.aggregations.Aggregations) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) IntervalTimeConfiguration(org.opensearch.ad.model.IntervalTimeConfiguration) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) Aggregation(org.opensearch.search.aggregations.Aggregation) NumericMetricsAggregation(org.opensearch.search.aggregations.metrics.NumericMetricsAggregation) MultiBucketsAggregation(org.opensearch.search.aggregations.bucket.MultiBucketsAggregation) ActionListener(org.opensearch.action.ActionListener) ExecutorService(java.util.concurrent.ExecutorService) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) LinearUniformInterpolator(org.opensearch.ad.dataprocessor.LinearUniformInterpolator) SingleFeatureLinearUniformInterpolator(org.opensearch.ad.dataprocessor.SingleFeatureLinearUniformInterpolator) SearchHits(org.opensearch.search.SearchHits) HashSet(java.util.HashSet) Before(org.junit.Before)

Example 7 with Aggregation

use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.

the class MatrixStatsNamedXContentProvider method getNamedXContentParsers.

@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
    ParseField parseField = new ParseField(MatrixStatsAggregationBuilder.NAME);
    ContextParser<Object, Aggregation> contextParser = (p, name) -> ParsedMatrixStats.fromXContent(p, (String) name);
    return singletonList(new NamedXContentRegistry.Entry(Aggregation.class, parseField, contextParser));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) List(java.util.List) Aggregation(org.opensearch.search.aggregations.Aggregation) ParsedMatrixStats(org.opensearch.search.aggregations.matrix.stats.ParsedMatrixStats) MatrixStatsAggregationBuilder(org.opensearch.search.aggregations.matrix.stats.MatrixStatsAggregationBuilder) NamedXContentProvider(org.opensearch.plugins.spi.NamedXContentProvider) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ContextParser(org.opensearch.common.xcontent.ContextParser) ParseField(org.opensearch.common.ParseField) Collections.singletonList(java.util.Collections.singletonList) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ParseField(org.opensearch.common.ParseField)

Example 8 with Aggregation

use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.

the class InternalSingleBucketAggregation method reducePipelines.

/**
 * Amulti-bucket agg needs to first reduce the buckets and *their* pipelines
 * before allowing sibling pipelines to materialize.
 */
@Override
public final InternalAggregation reducePipelines(InternalAggregation reducedAggs, ReduceContext reduceContext, PipelineTree pipelineTree) {
    assert reduceContext.isFinalReduce();
    InternalAggregation reduced = this;
    if (pipelineTree.hasSubTrees()) {
        List<InternalAggregation> aggs = new ArrayList<>();
        for (Aggregation agg : getAggregations().asList()) {
            PipelineTree subTree = pipelineTree.subTree(agg.getName());
            aggs.add(((InternalAggregation) agg).reducePipelines((InternalAggregation) agg, reduceContext, subTree));
        }
        InternalAggregations reducedSubAggs = InternalAggregations.from(aggs);
        reduced = create(reducedSubAggs);
    }
    return super.reducePipelines(reduced, reduceContext, pipelineTree);
}
Also used : InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) Aggregation(org.opensearch.search.aggregations.Aggregation) InternalAggregation(org.opensearch.search.aggregations.InternalAggregation) InternalAggregations(org.opensearch.search.aggregations.InternalAggregations) ArrayList(java.util.ArrayList) PipelineTree(org.opensearch.search.aggregations.pipeline.PipelineAggregator.PipelineTree)

Example 9 with Aggregation

use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.

the class ChildrenToParentAggregatorTests method testNoDocs.

public void testNoDocs() throws IOException {
    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory);
    // intentionally not writing any docs
    indexWriter.close();
    IndexReader indexReader = DirectoryReader.open(directory);
    testCase(new MatchAllDocsQuery(), newSearcher(indexReader, false, true), childrenToParent -> {
        assertEquals(0, childrenToParent.getDocCount());
        Aggregation parentAggregation = childrenToParent.getAggregations().get("in_parent");
        assertEquals(0, childrenToParent.getDocCount());
        assertNotNull("Aggregations: " + childrenToParent.getAggregations().asMap(), parentAggregation);
        assertEquals(Double.POSITIVE_INFINITY, ((InternalMin) parentAggregation).getValue(), Double.MIN_VALUE);
        assertFalse(JoinAggregationInspectionHelper.hasValue(childrenToParent));
    });
    indexReader.close();
    directory.close();
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) IndexReader(org.apache.lucene.index.IndexReader) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.tests.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 10 with Aggregation

use of org.opensearch.search.aggregations.Aggregation in project OpenSearch by opensearch-project.

the class ParentJoinNamedXContentProvider method getNamedXContentParsers.

@Override
public List<NamedXContentRegistry.Entry> getNamedXContentParsers() {
    ParseField parseFieldChildren = new ParseField(ChildrenAggregationBuilder.NAME);
    ParseField parseFieldParent = new ParseField(ParentAggregationBuilder.NAME);
    ContextParser<Object, Aggregation> contextParserChildren = (p, name) -> ParsedChildren.fromXContent(p, (String) name);
    ContextParser<Object, Aggregation> contextParserParent = (p, name) -> ParsedParent.fromXContent(p, (String) name);
    return Arrays.asList(new NamedXContentRegistry.Entry(Aggregation.class, parseFieldChildren, contextParserChildren), new NamedXContentRegistry.Entry(Aggregation.class, parseFieldParent, contextParserParent));
}
Also used : Aggregation(org.opensearch.search.aggregations.Aggregation) Arrays(java.util.Arrays) List(java.util.List) ParentAggregationBuilder(org.opensearch.join.aggregations.ParentAggregationBuilder) ParsedParent(org.opensearch.join.aggregations.ParsedParent) Aggregation(org.opensearch.search.aggregations.Aggregation) NamedXContentProvider(org.opensearch.plugins.spi.NamedXContentProvider) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ContextParser(org.opensearch.common.xcontent.ContextParser) ChildrenAggregationBuilder(org.opensearch.join.aggregations.ChildrenAggregationBuilder) ParsedChildren(org.opensearch.join.aggregations.ParsedChildren) ParseField(org.opensearch.common.ParseField) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) ParseField(org.opensearch.common.ParseField)

Aggregations

Aggregation (org.opensearch.search.aggregations.Aggregation)54 SearchResponse (org.opensearch.action.search.SearchResponse)31 HashMap (java.util.HashMap)30 List (java.util.List)30 ArrayList (java.util.ArrayList)29 Map (java.util.Map)20 InternalAggregation (org.opensearch.search.aggregations.InternalAggregation)19 Aggregations (org.opensearch.search.aggregations.Aggregations)18 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)16 Matchers.containsString (org.hamcrest.Matchers.containsString)14 IOException (java.io.IOException)13 Collections (java.util.Collections)13 ActionListener (org.opensearch.action.ActionListener)13 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)13 MultiBucketsAggregation (org.opensearch.search.aggregations.bucket.MultiBucketsAggregation)13 Terms (org.opensearch.search.aggregations.bucket.terms.Terms)13 Collectors (java.util.stream.Collectors)12 Script (org.opensearch.script.Script)12 Arrays (java.util.Arrays)11 SearchRequest (org.opensearch.action.search.SearchRequest)11