|
| 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.stats; |
| 10 | + |
| 11 | +import org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest; |
| 12 | +import org.opensearch.action.admin.indices.stats.IndicesStatsResponse; |
| 13 | +import org.opensearch.action.search.SearchResponse; |
| 14 | +import org.opensearch.cluster.metadata.IndexMetadata; |
| 15 | +import org.opensearch.common.settings.Settings; |
| 16 | +import org.opensearch.common.xcontent.XContentFactory; |
| 17 | +import org.opensearch.index.search.stats.SearchStats; |
| 18 | +import org.opensearch.search.aggregations.AggregationBuilders; |
| 19 | +import org.opensearch.search.aggregations.bucket.terms.Terms; |
| 20 | +import org.opensearch.search.aggregations.metrics.Sum; |
| 21 | +import org.opensearch.test.OpenSearchIntegTestCase; |
| 22 | + |
| 23 | +import static org.opensearch.index.query.QueryBuilders.matchAllQuery; |
| 24 | +import static org.opensearch.index.query.QueryBuilders.termQuery; |
| 25 | +import static org.hamcrest.Matchers.equalTo; |
| 26 | +import static org.hamcrest.Matchers.greaterThanOrEqualTo; |
| 27 | +import static org.hamcrest.Matchers.lessThanOrEqualTo; |
| 28 | + |
| 29 | +/** |
| 30 | + * Integration tests for search statistics related to star-tree. |
| 31 | + */ |
| 32 | +@OpenSearchIntegTestCase.ClusterScope(scope = OpenSearchIntegTestCase.Scope.SUITE) |
| 33 | +public class StarTreeSearchStatsIT extends OpenSearchIntegTestCase { |
| 34 | + |
| 35 | + private static final String STARTREE_INDEX_NAME = "test_startree"; |
| 36 | + private static final String REGULAR_INDEX_NAME = "test_regular"; |
| 37 | + |
| 38 | + /** |
| 39 | + * This test validates that star-tree specific search stats are correctly reported. |
| 40 | + * It creates two indices: one with a star-tree field and one without. |
| 41 | + * It then runs queries that should and should not be offloaded to the star-tree |
| 42 | + * and verifies the star-tree related stats. |
| 43 | + */ |
| 44 | + public void testStarTreeQueryStats() throws Exception { |
| 45 | + // Create an index with a star-tree field mapping using the composite structure |
| 46 | + createIndex( |
| 47 | + STARTREE_INDEX_NAME, |
| 48 | + Settings.builder() |
| 49 | + .put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1) |
| 50 | + .put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0) |
| 51 | + .put("index.composite_index", true) |
| 52 | + .put("index.append_only.enabled", true) |
| 53 | + .put("index.search.star_tree_index.enabled", true) |
| 54 | + .put("index.codec", "default") |
| 55 | + .build(), |
| 56 | + XContentFactory.jsonBuilder() |
| 57 | + .startObject() |
| 58 | + .startObject("composite") |
| 59 | + .startObject("my_star_tree") |
| 60 | + .field("type", "star_tree") |
| 61 | + .startObject("config") |
| 62 | + .startArray("ordered_dimensions") |
| 63 | + .startObject() |
| 64 | + .field("name", "product") |
| 65 | + .endObject() |
| 66 | + .startObject() |
| 67 | + .field("name", "size") |
| 68 | + .endObject() |
| 69 | + .endArray() |
| 70 | + .startArray("metrics") |
| 71 | + .startObject() |
| 72 | + .field("name", "sales") |
| 73 | + .field("stats", new String[] { "sum" }) |
| 74 | + .endObject() |
| 75 | + .endArray() |
| 76 | + .endObject() |
| 77 | + .endObject() |
| 78 | + .endObject() |
| 79 | + .startObject("properties") |
| 80 | + .startObject("product") |
| 81 | + .field("type", "keyword") |
| 82 | + .endObject() |
| 83 | + .startObject("size") |
| 84 | + .field("type", "keyword") |
| 85 | + .endObject() |
| 86 | + .startObject("sales") |
| 87 | + .field("type", "double") |
| 88 | + .endObject() |
| 89 | + .startObject("non_dimension_field") |
| 90 | + .field("type", "text") |
| 91 | + .endObject() |
| 92 | + .endObject() |
| 93 | + .endObject() |
| 94 | + .toString() |
| 95 | + ); |
| 96 | + |
| 97 | + // Create an index without a star-tree |
| 98 | + createIndex( |
| 99 | + REGULAR_INDEX_NAME, |
| 100 | + Settings.builder().put(IndexMetadata.SETTING_NUMBER_OF_SHARDS, 1).put(IndexMetadata.SETTING_NUMBER_OF_REPLICAS, 0).build() |
| 101 | + ); |
| 102 | + ensureGreen(STARTREE_INDEX_NAME, REGULAR_INDEX_NAME); |
| 103 | + |
| 104 | + client().prepareIndex(STARTREE_INDEX_NAME) |
| 105 | + .setSource("product", "laptop", "sales", 1200.0, "non_dimension_field", "some text", "size", "18") |
| 106 | + .get(); |
| 107 | + client().prepareIndex(STARTREE_INDEX_NAME) |
| 108 | + .setSource("product", "keyboard", "sales", 150.0, "non_dimension_field", "more text", "size", "12") |
| 109 | + .get(); |
| 110 | + client().prepareIndex(REGULAR_INDEX_NAME).setSource("product", "monitor", "sales", 400.0).get(); |
| 111 | + |
| 112 | + // Force merge the star-tree index to ensure star-tree segments are created |
| 113 | + client().admin().indices().forceMerge(new ForceMergeRequest(STARTREE_INDEX_NAME).maxNumSegments(1)).get(); |
| 114 | + refresh(); |
| 115 | + |
| 116 | + // Query 1: This query should be answered by the star-tree. |
| 117 | + // It's an aggregation on a dimension with a metric sub-aggregation and size=0. |
| 118 | + SearchResponse starTreeResponse = client().prepareSearch(STARTREE_INDEX_NAME) |
| 119 | + .setQuery(matchAllQuery()) |
| 120 | + .addAggregation( |
| 121 | + AggregationBuilders.terms("products").field("product").subAggregation(AggregationBuilders.sum("total_sales").field("sales")) |
| 122 | + ) |
| 123 | + .setSize(0) |
| 124 | + .execute() |
| 125 | + .actionGet(); |
| 126 | + assertEquals(2, starTreeResponse.getHits().getTotalHits().value()); |
| 127 | + Terms terms = starTreeResponse.getAggregations().get("products"); |
| 128 | + assertEquals(2, terms.getBuckets().size()); |
| 129 | + Sum sum = terms.getBuckets().get(0).getAggregations().get("total_sales"); |
| 130 | + assertNotNull(sum); |
| 131 | + |
| 132 | + // Query 2: This query should NOT be answered by the star-tree as it queries a non-dimension field. |
| 133 | + client().prepareSearch(STARTREE_INDEX_NAME).setQuery(termQuery("non_dimension_field", "text")).execute().actionGet(); |
| 134 | + |
| 135 | + // Query 3: A standard query on the regular index. |
| 136 | + client().prepareSearch(REGULAR_INDEX_NAME).setQuery(matchAllQuery()).get(); |
| 137 | + |
| 138 | + // Fetch index statistics |
| 139 | + IndicesStatsResponse stats = client().admin().indices().prepareStats(STARTREE_INDEX_NAME, REGULAR_INDEX_NAME).setSearch(true).get(); |
| 140 | + |
| 141 | + // Assertions for the star-tree index |
| 142 | + SearchStats.Stats starTreeIndexStats = stats.getIndex(STARTREE_INDEX_NAME).getTotal().getSearch().getTotal(); |
| 143 | + |
| 144 | + assertThat("Star-tree index should have 2 total queries", starTreeIndexStats.getQueryCount(), equalTo(2L)); |
| 145 | + assertThat("Star-tree index should have 1 star-tree query", starTreeIndexStats.getStarTreeQueryCount(), equalTo(1L)); |
| 146 | + assertThat( |
| 147 | + "Star-tree query time should be non-negative", |
| 148 | + starTreeIndexStats.getStarTreeQueryTimeInMillis(), |
| 149 | + greaterThanOrEqualTo(0L) |
| 150 | + ); |
| 151 | + assertThat( |
| 152 | + "Star-tree query current should be non-negative", |
| 153 | + starTreeIndexStats.getStarTreeQueryCurrent(), |
| 154 | + greaterThanOrEqualTo(0L) |
| 155 | + ); |
| 156 | + |
| 157 | + // Assertions for the regular index |
| 158 | + SearchStats.Stats regularIndexStats = stats.getIndex(REGULAR_INDEX_NAME).getTotal().getSearch().getTotal(); |
| 159 | + assertThat("Regular index should have 1 total query", regularIndexStats.getQueryCount(), equalTo(1L)); |
| 160 | + assertThat("Regular index should have 0 star-tree queries", regularIndexStats.getStarTreeQueryCount(), equalTo(0L)); |
| 161 | + assertThat("Regular index should have 0 star-tree query time", regularIndexStats.getStarTreeQueryTimeInMillis(), equalTo(0L)); |
| 162 | + |
| 163 | + // Assertions for the total stats across both indices |
| 164 | + SearchStats.Stats totalStats = stats.getTotal().getSearch().getTotal(); |
| 165 | + assertThat("Total query count should be 3", totalStats.getQueryCount(), equalTo(3L)); |
| 166 | + assertThat("Total star-tree query count should be 1", totalStats.getStarTreeQueryCount(), equalTo(1L)); |
| 167 | + assertThat(totalStats.getQueryTimeInMillis(), greaterThanOrEqualTo(totalStats.getStarTreeQueryTimeInMillis())); |
| 168 | + |
| 169 | + // While not guaranteed, the time spent on the single star-tree query should be less than the total query time. |
| 170 | + if (totalStats.getStarTreeQueryCount() > 0 && totalStats.getQueryCount() > totalStats.getStarTreeQueryCount()) { |
| 171 | + assertThat( |
| 172 | + "Star-tree query time should be less than total query time", |
| 173 | + totalStats.getStarTreeQueryTimeInMillis(), |
| 174 | + lessThanOrEqualTo(totalStats.getQueryTimeInMillis()) |
| 175 | + ); |
| 176 | + } |
| 177 | + } |
| 178 | +} |
0 commit comments