Search in sources :

Example 16 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project OpenSearch by opensearch-project.

the class FiltersTests method testFiltersSortedByKey.

/**
 * Test that when passing in keyed filters as list or array, the list stored internally is sorted by key
 * Also check the list passed in is not modified by this but rather copied
 */
public void testFiltersSortedByKey() {
    KeyedFilter[] original = new KeyedFilter[] { new KeyedFilter("bbb", new MatchNoneQueryBuilder()), new KeyedFilter("aaa", new MatchNoneQueryBuilder()) };
    FiltersAggregationBuilder builder;
    builder = new FiltersAggregationBuilder("my-agg", original);
    assertEquals("aaa", builder.filters().get(0).key());
    assertEquals("bbb", builder.filters().get(1).key());
    // original should be unchanged
    assertEquals("bbb", original[0].key());
    assertEquals("aaa", original[1].key());
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) MatchNoneQueryBuilder(org.opensearch.index.query.MatchNoneQueryBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder)

Example 17 with KeyedFilter

use of org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter in project ml-commons by opensearch-project.

the class AnomalyLocalizerImpl method newSearchRequestForEntityKeys.

private SearchRequest newSearchRequestForEntityKeys(AnomalyLocalizationInput input, AggregationBuilder agg, AnomalyLocalizationOutput.Bucket bucket, List<List<String>> keys) {
    RangeQueryBuilder timeRangeFilter = new RangeQueryBuilder(input.getTimeFieldName()).from(bucket.getBase().get().getStartTime(), true).to(bucket.getBase().get().getEndTime(), true);
    BoolQueryBuilder filter = QueryBuilders.boolQuery().filter(timeRangeFilter);
    input.getFilterQuery().ifPresent(q -> filter.filter(q));
    KeyedFilter[] filters = IntStream.range(0, keys.size()).mapToObj(i -> new KeyedFilter(Integer.toString(i), newQueryByKey(keys.get(i), input))).toArray(KeyedFilter[]::new);
    FiltersAggregationBuilder filtersAgg = AggregationBuilders.filters(agg.getName(), filters);
    filtersAgg.subAggregation(agg);
    SearchSourceBuilder search = new SearchSourceBuilder().size(0).query(filter).aggregation(filtersAgg);
    SearchRequest searchRequest = new SearchRequest(new String[] { input.getIndexName() }, search);
    return searchRequest;
}
Also used : KeyedFilter(org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter) 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) BoolQueryBuilder(org.opensearch.index.query.BoolQueryBuilder) FiltersAggregationBuilder(org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder) RangeQueryBuilder(org.opensearch.index.query.RangeQueryBuilder) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder)

Aggregations

KeyedFilter (org.opensearch.search.aggregations.bucket.filter.FiltersAggregator.KeyedFilter)17 SearchResponse (org.opensearch.action.search.SearchResponse)9 Filters (org.opensearch.search.aggregations.bucket.filter.Filters)9 OpenSearchAssertions.assertSearchResponse (org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse)8 BoolQueryBuilder (org.opensearch.index.query.BoolQueryBuilder)4 QueryBuilder (org.opensearch.index.query.QueryBuilder)4 FiltersAggregationBuilder (org.opensearch.search.aggregations.bucket.filter.FiltersAggregationBuilder)4 ArrayList (java.util.ArrayList)3 AbstractQueryBuilder.parseInnerQueryBuilder (org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder)2 MatchAllQueryBuilder (org.opensearch.index.query.MatchAllQueryBuilder)2 MatchNoneQueryBuilder (org.opensearch.index.query.MatchNoneQueryBuilder)2 Histogram (org.opensearch.search.aggregations.bucket.histogram.Histogram)2 Avg (org.opensearch.search.aggregations.metrics.Avg)2 SimpleEntry (java.util.AbstractMap.SimpleEntry)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