|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * |
| 4 | + * The OpenSearch Contributors require contributions made to |
| 5 | + * this file be licensed under the Apache-2.0 license or a |
| 6 | + * compatible open source license. |
| 7 | + */ |
| 8 | + |
| 9 | +package org.opensearch.search.aggregations.bucket; |
| 10 | + |
| 11 | +import com.carrotsearch.randomizedtesting.annotations.ParametersFactory; |
| 12 | + |
| 13 | +import org.opensearch.action.search.SearchResponse; |
| 14 | +import org.opensearch.cluster.health.ClusterHealthStatus; |
| 15 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 16 | +import org.opensearch.common.settings.Settings; |
| 17 | +import org.opensearch.search.aggregations.bucket.composite.CompositeAggregationBuilder; |
| 18 | +import org.opensearch.search.aggregations.bucket.composite.CompositeValuesSourceBuilder; |
| 19 | +import org.opensearch.search.aggregations.bucket.composite.TermsValuesSourceBuilder; |
| 20 | +import org.opensearch.search.aggregations.metrics.MaxAggregationBuilder; |
| 21 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 22 | +import org.opensearch.test.ParameterizedStaticSettingsOpenSearchIntegTestCase; |
| 23 | + |
| 24 | +import java.util.ArrayList; |
| 25 | +import java.util.Arrays; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import static org.opensearch.search.SearchService.CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING; |
| 30 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertAcked; |
| 31 | +import static org.opensearch.test.hamcrest.OpenSearchAssertions.assertSearchResponse; |
| 32 | + |
| 33 | +@OpenSearchIntegTestCase.SuiteScopeTestCase |
| 34 | +public class CompositeAggIT extends ParameterizedStaticSettingsOpenSearchIntegTestCase { |
| 35 | + |
| 36 | + public CompositeAggIT(Settings staticSettings) { |
| 37 | + super(staticSettings); |
| 38 | + } |
| 39 | + |
| 40 | + @ParametersFactory |
| 41 | + public static Collection<Object[]> parameters() { |
| 42 | + return Arrays.asList( |
| 43 | + new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), false).build() }, |
| 44 | + new Object[] { Settings.builder().put(CLUSTER_CONCURRENT_SEGMENT_SEARCH_SETTING.getKey(), true).build() } |
| 45 | + ); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void setupSuiteScopeCluster() throws Exception { |
| 50 | + assertAcked( |
| 51 | + prepareCreate( |
| 52 | + "idx", |
| 53 | + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 54 | + ).setMapping("type", "type=keyword", "num", "type=integer", "score", "type=integer") |
| 55 | + ); |
| 56 | + waitForRelocation(ClusterHealthStatus.GREEN); |
| 57 | + |
| 58 | + client().prepareIndex("idx").setId("1").setSource("type", "type1", "num", "1", "score", "5").get(); |
| 59 | + client().prepareIndex("idx").setId("1").setSource("type", "type2", "num", "11", "score", "50").get(); |
| 60 | + refresh("idx"); |
| 61 | + client().prepareIndex("idx").setId("1").setSource("type", "type1", "num", "1", "score", "2").get(); |
| 62 | + client().prepareIndex("idx").setId("1").setSource("type", "type2", "num", "12", "score", "20").get(); |
| 63 | + refresh("idx"); |
| 64 | + client().prepareIndex("idx").setId("1").setSource("type", "type1", "num", "3", "score", "10").get(); |
| 65 | + client().prepareIndex("idx").setId("1").setSource("type", "type2", "num", "13", "score", "15").get(); |
| 66 | + refresh("idx"); |
| 67 | + client().prepareIndex("idx").setId("1").setSource("type", "type1", "num", "3", "score", "1").get(); |
| 68 | + client().prepareIndex("idx").setId("1").setSource("type", "type2", "num", "13", "score", "100").get(); |
| 69 | + refresh("idx"); |
| 70 | + |
| 71 | + waitForRelocation(ClusterHealthStatus.GREEN); |
| 72 | + refresh(); |
| 73 | + } |
| 74 | + |
| 75 | + public void testCompositeAggWithNoSubAgg() { |
| 76 | + SearchResponse rsp = client().prepareSearch("idx") |
| 77 | + .addAggregation(new CompositeAggregationBuilder("my_composite", getTestValueSources())) |
| 78 | + .get(); |
| 79 | + assertSearchResponse(rsp); |
| 80 | + } |
| 81 | + |
| 82 | + public void testCompositeAggWithSubAgg() { |
| 83 | + SearchResponse rsp = client().prepareSearch("idx") |
| 84 | + .addAggregation( |
| 85 | + new CompositeAggregationBuilder("my_composite", getTestValueSources()).subAggregation( |
| 86 | + new MaxAggregationBuilder("max").field("score") |
| 87 | + ) |
| 88 | + ) |
| 89 | + .get(); |
| 90 | + assertSearchResponse(rsp); |
| 91 | + } |
| 92 | + |
| 93 | + private List<CompositeValuesSourceBuilder<?>> getTestValueSources() { |
| 94 | + final List<CompositeValuesSourceBuilder<?>> sources = new ArrayList<>(); |
| 95 | + sources.add(new TermsValuesSourceBuilder("keyword_vs").field("type")); |
| 96 | + sources.add(new TermsValuesSourceBuilder("num_vs").field("num")); |
| 97 | + return sources; |
| 98 | + } |
| 99 | +} |
0 commit comments