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);
}
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));
}
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);
}
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();
}
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));
}
Aggregations