Skip to content

Commit ccfd35f

Browse files
author
Kishore Kumaar Natarajan
committed
Checking index content and index template content
Signed-off-by: Kishore Kumaar Natarajan <[email protected]>
1 parent 3d3c284 commit ccfd35f

File tree

1 file changed

+53
-73
lines changed

1 file changed

+53
-73
lines changed

src/test/java/org/opensearch/plugin/insights/core/exporter/QueryInsightsExporterIT.java

+53-73
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,18 @@
99
package org.opensearch.plugin.insights.core.exporter;
1010

1111
import java.io.IOException;
12+
import java.nio.charset.StandardCharsets;
1213
import org.junit.Assert;
1314
import org.opensearch.client.Request;
1415
import org.opensearch.client.Response;
1516
import org.opensearch.client.ResponseException;
1617
import org.opensearch.plugin.insights.QueryInsightsRestTestCase;
17-
import java.io.InputStream;
18-
import java.nio.charset.StandardCharsets;
1918

2019
/** Rest Action tests for query */
2120
public class QueryInsightsExporterIT extends QueryInsightsRestTestCase {
2221

23-
/**
24-
* Test Top Queries setting endpoints with document creation, search, local index exporter control,
25-
* and window size configuration.
26-
*
27-
* @throws IOException IOException
28-
*/
2922
public void testQueryInsightsExporterSettings() throws IOException, InterruptedException {
23+
3024
createDocument();
3125

3226
for (String setting : invalidExporterSettings()) {
@@ -48,16 +42,31 @@ public void testQueryInsightsExporterSettings() throws IOException, InterruptedE
4842
performSearch();
4943
waitForWindowToPass(70);
5044
checkLocalIndices("After search and waiting for data export");
45+
checkQueryInsightsIndexTemplate();
5146
disableLocalIndexExporter();
5247
reEnableLocalIndexExporter();
5348
setLocalIndexToDebug();
5449
}
5550

51+
private void performSearch() throws IOException {
52+
String searchJson = "{\n" + " \"query\": {\n" + " \"term\": {\n" + " \"user.id\": \"cyji\"\n" + " }\n" + " }\n" + "}";
53+
54+
Request searchRequest = new Request("POST", "/my-index-0/_search");
55+
searchRequest.setJsonEntity(searchJson);
56+
Response response = client().performRequest(searchRequest);
57+
58+
assertEquals(200, response.getStatusLine().getStatusCode());
59+
60+
String responseContent = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
61+
62+
assertTrue("Expected search results for user.id='cyji'", responseContent.contains("\"hits\":"));
63+
}
5664

5765
private void createDocument() throws IOException {
5866
String documentJson = "{\n"
59-
+ " \"title\": \"Test Document\",\n"
60-
+ " \"content\": \"This is a test document for OpenSearch\"\n"
67+
+ " \"@timestamp\": \"2099-11-15T13:12:00\",\n"
68+
+ " \"message\": \"this is document 0\",\n"
69+
+ " \"user\": { \"id\": \"cyji\", \"age\": 1 }\n"
6170
+ "}";
6271

6372
Request createDocumentRequest = new Request("POST", "/my-index-0/_doc/");
@@ -66,103 +75,74 @@ private void createDocument() throws IOException {
6675
assertEquals(201, response.getStatusLine().getStatusCode());
6776
}
6877

69-
private void performSearch() throws IOException {
70-
String searchJson = "{\n"
71-
+ " \"query\": {\n"
72-
+ " \"match\": {\n"
73-
+ " \"content\": \"test\"\n"
74-
+ " }\n"
75-
+ " }\n"
76-
+ "}";
77-
Request searchRequest = new Request("POST", "/my-index-0/_search");
78-
searchRequest.setJsonEntity(searchJson);
79-
Response response = client().performRequest(searchRequest);
80-
assertEquals(200, response.getStatusLine().getStatusCode());
81-
}
82-
8378
private void checkLocalIndices(String context) throws IOException {
8479
Request indicesRequest = new Request("GET", "/_cat/indices?v");
8580
Response response = client().performRequest(indicesRequest);
8681
assertEquals(200, response.getStatusLine().getStatusCode());
87-
InputStream responseStream = response.getEntity().getContent();
88-
String responseContent = new String(responseStream.readAllBytes(), StandardCharsets.UTF_8);
89-
System.out.println("Response content for " + context + ": " + responseContent);
90-
assertTrue("Expected top_queries-* index to be present in the response",
91-
responseContent.contains("top_queries-"));
9282

93-
}
83+
String responseContent = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
84+
assertTrue("Expected top_queries-* index to be present", responseContent.contains("top_queries-"));
9485

86+
// Fetch and check documents in top_queries-* index
87+
Request fetchRequest = new Request("GET", "/top_queries-*/_search?size=10");
88+
Response fetchResponse = client().performRequest(fetchRequest);
89+
assertEquals(200, fetchResponse.getStatusLine().getStatusCode());
9590

91+
String fetchResponseContent = new String(fetchResponse.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
92+
assertTrue(
93+
"Expected user.id field with value 'cyji' in query insights data",
94+
fetchResponseContent.contains("\"user.id\":{\"value\":\"cyji\",\"boost\":1.0}")
95+
);
96+
}
97+
98+
private void checkQueryInsightsIndexTemplate() throws IOException {
99+
Request request = new Request("GET", "/_index_template");
100+
Response response = client().performRequest(request);
101+
String responseContent = new String(response.getEntity().getContent().readAllBytes(), StandardCharsets.UTF_8);
102+
assertTrue(
103+
"Expected default index template for Query Insights to be present",
104+
responseContent.contains("\"query_insights_top_queries_template\"")
105+
);
106+
}
96107

97108
private void disableLocalIndexExporter() throws IOException {
98-
String disableExporterJson = "{\n"
99-
+ " \"persistent\": {\n"
100-
+ " \"search.insights.top_queries.exporter.type\": \"none\"\n"
101-
+ " }\n"
102-
+ "}";
109+
String disableExporterJson = "{ \"persistent\": { \"search.insights.top_queries.exporter.type\": \"none\" } }";
103110
Request disableExporterRequest = new Request("PUT", "/_cluster/settings");
104111
disableExporterRequest.setJsonEntity(disableExporterJson);
105-
Response response = client().performRequest(disableExporterRequest);
106-
assertEquals(200, response.getStatusLine().getStatusCode());
112+
client().performRequest(disableExporterRequest);
107113
}
108114

109115
private void reEnableLocalIndexExporter() throws IOException {
110-
String enableExporterJson = "{\n"
111-
+ " \"persistent\": {\n"
112-
+ " \"search.insights.top_queries.exporter.type\": \"local_index\"\n"
113-
+ " }\n"
114-
+ "}";
115116
Request enableExporterRequest = new Request("PUT", "/_cluster/settings");
116-
enableExporterRequest.setJsonEntity(enableExporterJson);
117-
Response response = client().performRequest(enableExporterRequest);
118-
assertEquals(200, response.getStatusLine().getStatusCode());
117+
enableExporterRequest.setJsonEntity(defaultExporterSettings());
118+
client().performRequest(enableExporterRequest);
119119
}
120120

121121
private void setLocalIndexToDebug() throws IOException {
122-
String debugExporterJson = "{\n"
123-
+ " \"persistent\": {\n"
124-
+ " \"search.insights.top_queries.exporter.type\": \"debug\"\n"
125-
+ " }\n"
126-
+ "}";
122+
String debugExporterJson = "{ \"persistent\": { \"search.insights.top_queries.exporter.type\": \"debug\" } }";
127123
Request debugExporterRequest = new Request("PUT", "/_cluster/settings");
128124
debugExporterRequest.setJsonEntity(debugExporterJson);
129-
Response response = client().performRequest(debugExporterRequest);
130-
assertEquals(200, response.getStatusLine().getStatusCode());
125+
client().performRequest(debugExporterRequest);
131126
}
132127

133128
private void setLatencyWindowSize(String windowSize) throws IOException {
134-
String windowSizeJson = "{\n"
135-
+ " \"persistent\": {\n"
136-
+ " \"search.insights.top_queries.latency.window_size\": \"" + windowSize + "\"\n"
137-
+ " }\n"
138-
+ "}";
129+
String windowSizeJson = "{ \"persistent\": { \"search.insights.top_queries.latency.window_size\": \"" + windowSize + "\" } }";
139130
Request windowSizeRequest = new Request("PUT", "/_cluster/settings");
140131
windowSizeRequest.setJsonEntity(windowSizeJson);
141-
Response response = client().performRequest(windowSizeRequest);
142-
assertEquals(200, response.getStatusLine().getStatusCode());
132+
client().performRequest(windowSizeRequest);
143133
}
144134

145135
private void waitForWindowToPass(int seconds) throws InterruptedException {
146-
System.out.println("Waiting for " + seconds + " seconds for window size to pass...");
147-
Thread.sleep(seconds * 1000); // Sleep for the specified number of seconds
136+
Thread.sleep(seconds * 1000);
148137
}
149138

150139
private String defaultExporterSettings() {
151-
return "{\n"
152-
+ " \"persistent\" : {\n"
153-
+ " \"search.insights.top_queries.exporter.type\" : \"local_index\"\n"
154-
+ " }\n"
155-
+ "}";
140+
return "{ \"persistent\" : { \"search.insights.top_queries.exporter.type\" : \"local_index\" } }";
156141
}
157142

158143
private String[] invalidExporterSettings() {
159144
return new String[] {
160-
"{\n" + " \"persistent\" : {\n" + " \"search.insights.top_queries.exporter.type\" : invalid_type\n" + " }\n" + "}",
161-
"{\n"
162-
+ " \"persistent\" : {\n"
163-
+ " \"search.insights.top_queries.exporter.type\" : local_index,\n"
164-
+ " \"search.insights.top_queries.exporter.config.index\" : \"1a2b\"\n"
165-
+ " }\n"
166-
+ "}" };
145+
"{ \"persistent\" : { \"search.insights.top_queries.exporter.type\" : invalid_type } }",
146+
"{ \"persistent\" : { \"search.insights.top_queries.exporter.type\" : local_index, \"search.insights.top_queries.exporter.config.index\" : \"1a2b\" } }" };
167147
}
168148
}

0 commit comments

Comments
 (0)