|
| 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 | +package org.opensearch.plugin.insights.core.service.grouper; |
| 9 | + |
| 10 | +import java.io.IOException; |
| 11 | +import java.nio.charset.StandardCharsets; |
| 12 | +import org.junit.Assert; |
| 13 | +import org.opensearch.client.Request; |
| 14 | +import org.opensearch.client.Response; |
| 15 | +import org.opensearch.client.ResponseException; |
| 16 | +import org.opensearch.plugin.insights.QueryInsightsRestTestCase; |
| 17 | +import org.opensearch.plugin.insights.settings.QueryInsightsSettings; |
| 18 | + |
| 19 | +/** |
| 20 | + * ITs for Grouping Top Queries by similarity |
| 21 | + */ |
| 22 | +public class MinMaxQueryGrouperBySimilarityIT extends QueryInsightsRestTestCase { |
| 23 | + |
| 24 | + /** |
| 25 | + * test grouping top queries |
| 26 | + * |
| 27 | + * @throws IOException IOException |
| 28 | + */ |
| 29 | + public void testGroupingBySimilarity() throws IOException, InterruptedException { |
| 30 | + |
| 31 | + waitForEmptyTopQueriesResponse(); |
| 32 | + |
| 33 | + // Enable top N feature and grouping feature |
| 34 | + Request request = new Request("PUT", "/_cluster/settings"); |
| 35 | + request.setJsonEntity(defaultTopQueryGroupingSettings()); |
| 36 | + Response response = client().performRequest(request); |
| 37 | + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); |
| 38 | + |
| 39 | + // Search |
| 40 | + doSearch("range", 2); |
| 41 | + doSearch("match", 6); |
| 42 | + doSearch("term", 4); |
| 43 | + |
| 44 | + // run five times to make sure the records are drained to the top queries services |
| 45 | + for (int i = 0; i < 5; i++) { |
| 46 | + // Get Top Queries |
| 47 | + request = new Request("GET", "/_insights/top_queries?pretty"); |
| 48 | + response = client().performRequest(request); |
| 49 | + Assert.assertEquals(200, response.getStatusLine().getStatusCode()); |
| 50 | + |
| 51 | + String responseBody = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8); |
| 52 | + |
| 53 | + // Extract and count top_queries |
| 54 | + int topNArraySize = countTopQueries(responseBody); |
| 55 | + |
| 56 | + if (topNArraySize == 0) { |
| 57 | + Thread.sleep(QueryInsightsSettings.QUERY_RECORD_QUEUE_DRAIN_INTERVAL.millis()); |
| 58 | + continue; |
| 59 | + } |
| 60 | + |
| 61 | + Assert.assertEquals(3, topNArraySize); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Test invalid query grouping settings |
| 67 | + * |
| 68 | + * @throws IOException IOException |
| 69 | + */ |
| 70 | + public void testInvalidQueryGroupingSettings() throws IOException { |
| 71 | + for (String setting : invalidQueryGroupingSettings()) { |
| 72 | + Request request = new Request("PUT", "/_cluster/settings"); |
| 73 | + request.setJsonEntity(setting); |
| 74 | + try { |
| 75 | + client().performRequest(request); |
| 76 | + fail("Should not succeed with invalid query grouping settings"); |
| 77 | + } catch (ResponseException e) { |
| 78 | + assertEquals(400, e.getResponse().getStatusLine().getStatusCode()); |
| 79 | + } |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Test valid query grouping settings |
| 85 | + * |
| 86 | + * @throws IOException IOException |
| 87 | + */ |
| 88 | + public void testValidQueryGroupingSettings() throws IOException { |
| 89 | + for (String setting : validQueryGroupingSettings()) { |
| 90 | + Request request = new Request("PUT", "/_cluster/settings"); |
| 91 | + request.setJsonEntity(setting); |
| 92 | + Response response = client().performRequest(request); |
| 93 | + assertEquals(200, response.getStatusLine().getStatusCode()); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + private String[] invalidQueryGroupingSettings() { |
| 98 | + return new String[] { |
| 99 | + // Invalid max_groups: below minimum (-1) |
| 100 | + "{\n" |
| 101 | + + " \"persistent\" : {\n" |
| 102 | + + " \"search.insights.top_queries.max_groups_excluding_topn\" : -1\n" |
| 103 | + + " }\n" |
| 104 | + + "}", |
| 105 | + |
| 106 | + // Invalid max_groups: above maximum (10001) |
| 107 | + "{\n" |
| 108 | + + " \"persistent\" : {\n" |
| 109 | + + " \"search.insights.top_queries.max_groups_excluding_topn\" : 10001\n" |
| 110 | + + " }\n" |
| 111 | + + "}", |
| 112 | + |
| 113 | + // Invalid group_by: unsupported value |
| 114 | + "{\n" |
| 115 | + + " \"persistent\" : {\n" |
| 116 | + + " \"search.insights.top_queries.group_by\" : \"unsupported_value\"\n" |
| 117 | + + " }\n" |
| 118 | + + "}" }; |
| 119 | + } |
| 120 | + |
| 121 | + private String[] validQueryGroupingSettings() { |
| 122 | + return new String[] { |
| 123 | + // Valid max_groups: minimum value (0) |
| 124 | + "{\n" |
| 125 | + + " \"persistent\" : {\n" |
| 126 | + + " \"search.insights.top_queries.max_groups_excluding_topn\" : 0\n" |
| 127 | + + " }\n" |
| 128 | + + "}", |
| 129 | + |
| 130 | + // Valid max_groups: maximum value (10000) |
| 131 | + "{\n" |
| 132 | + + " \"persistent\" : {\n" |
| 133 | + + " \"search.insights.top_queries.max_groups_excluding_topn\" : 10000\n" |
| 134 | + + " }\n" |
| 135 | + + "}", |
| 136 | + |
| 137 | + // Valid group_by: supported value (SIMILARITY) |
| 138 | + "{\n" + " \"persistent\" : {\n" + " \"search.insights.top_queries.group_by\" : \"SIMILARITY\"\n" + " }\n" + "}" }; |
| 139 | + } |
| 140 | + |
| 141 | + private String groupByNoneSettings() { |
| 142 | + return "{\n" |
| 143 | + + " \"persistent\" : {\n" |
| 144 | + + " \"search.insights.top_queries.latency.enabled\" : \"true\",\n" |
| 145 | + + " \"search.insights.top_queries.latency.window_size\" : \"1m\",\n" |
| 146 | + + " \"search.insights.top_queries.latency.top_n_size\" : 100,\n" |
| 147 | + + " \"search.insights.top_queries.group_by\" : \"none\",\n" |
| 148 | + + " \"search.insights.top_queries.max_groups_excluding_topn\" : 5\n" |
| 149 | + + " }\n" |
| 150 | + + "}"; |
| 151 | + } |
| 152 | +} |
0 commit comments