Search in sources :

Example 1 with TermsValuesSourceBuilder

use of org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder in project OpenSearch by opensearch-project.

the class SearchIT method testSearchWithCompositeAgg.

public void testSearchWithCompositeAgg() throws IOException {
    SearchRequest searchRequest = new SearchRequest();
    SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
    List<CompositeValuesSourceBuilder<?>> sources = Collections.singletonList(new TermsValuesSourceBuilder("terms").field("type.keyword").missingBucket(true).order("asc"));
    searchSourceBuilder.aggregation(AggregationBuilders.composite("composite", sources));
    searchSourceBuilder.size(0);
    searchRequest.source(searchSourceBuilder);
    searchRequest.indices("index");
    SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
    assertSearchHeader(searchResponse);
    assertNull(searchResponse.getSuggest());
    assertEquals(Collections.emptyMap(), searchResponse.getProfileResults());
    assertEquals(0, searchResponse.getHits().getHits().length);
    assertEquals(Float.NaN, searchResponse.getHits().getMaxScore(), 0f);
    CompositeAggregation compositeAgg = searchResponse.getAggregations().get("composite");
    assertEquals("composite", compositeAgg.getName());
    assertEquals(2, compositeAgg.getBuckets().size());
    CompositeAggregation.Bucket bucket1 = compositeAgg.getBuckets().get(0);
    assertEquals(3, bucket1.getDocCount());
    assertEquals("{terms=type1}", bucket1.getKeyAsString());
    assertEquals(0, bucket1.getAggregations().asList().size());
    CompositeAggregation.Bucket bucket2 = compositeAgg.getBuckets().get(1);
    assertEquals(2, bucket2.getDocCount());
    assertEquals("{terms=type2}", bucket2.getKeyAsString());
    assertEquals(0, bucket2.getAggregations().asList().size());
}
Also used : MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) SearchRequest(org.opensearch.action.search.SearchRequest) TermsValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder) CompositeAggregation(org.opensearch.search.aggregations.bucket.composite.CompositeAggregation) CompositeValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) MultiSearchResponse(org.opensearch.action.search.MultiSearchResponse) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 2 with TermsValuesSourceBuilder

use of org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder in project ml-commons by opensearch-project.

the class AnomalyLocalizerImpl method newSearchRequestForEntry.

private SearchRequest newSearchRequestForEntry(AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Bucket bucket, Optional<Map<String, Object>> afterKey) {
    RangeQueryBuilder timeRangeFilter = new RangeQueryBuilder(input.getTimeFieldName()).from(bucket.getStartTime(), true).to(bucket.getEndTime(), true);
    BoolQueryBuilder filter = QueryBuilders.boolQuery().filter(timeRangeFilter);
    input.getFilterQuery().ifPresent(q -> filter.filter(q));
    CompositeAggregationBuilder compositeAgg = new CompositeAggregationBuilder(agg.getName(), input.getAttributeFieldNames().stream().map(name -> new TermsValuesSourceBuilder(name).field(name)).collect(Collectors.toList())).size(MAX_BUCKET_SETTING.get(this.settings));
    compositeAgg.subAggregation(agg);
    if (afterKey.isPresent()) {
        compositeAgg.aggregateAfter(afterKey.get());
    }
    SearchSourceBuilder search = new SearchSourceBuilder().size(0).query(filter).aggregation(compositeAgg);
    SearchRequest searchRequest = new SearchRequest(new String[] { input.getIndexName() }, search);
    return searchRequest;
}
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) TermsValuesSourceBuilder(org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) CompositeAggregationBuilder(org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)2 MultiSearchResponse (org.opensearch.action.search.MultiSearchResponse)2 SearchRequest (org.opensearch.action.search.SearchRequest)2 SearchResponse (org.opensearch.action.search.SearchResponse)2 CompositeAggregation (org.opensearch.search.aggregations.bucket.composite.CompositeAggregation)2 TermsValuesSourceBuilder (org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder)2 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