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