9
9
package org .opensearch .plugin .insights .core .exporter ;
10
10
11
11
import java .io .IOException ;
12
+ import java .nio .charset .StandardCharsets ;
12
13
import org .junit .Assert ;
13
14
import org .opensearch .client .Request ;
14
15
import org .opensearch .client .Response ;
15
16
import org .opensearch .client .ResponseException ;
16
17
import org .opensearch .plugin .insights .QueryInsightsRestTestCase ;
17
- import java .io .InputStream ;
18
- import java .nio .charset .StandardCharsets ;
19
18
20
19
/** Rest Action tests for query */
21
20
public class QueryInsightsExporterIT extends QueryInsightsRestTestCase {
22
21
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
- */
29
22
public void testQueryInsightsExporterSettings () throws IOException , InterruptedException {
23
+
30
24
createDocument ();
31
25
32
26
for (String setting : invalidExporterSettings ()) {
@@ -48,16 +42,31 @@ public void testQueryInsightsExporterSettings() throws IOException, InterruptedE
48
42
performSearch ();
49
43
waitForWindowToPass (70 );
50
44
checkLocalIndices ("After search and waiting for data export" );
45
+ checkQueryInsightsIndexTemplate ();
51
46
disableLocalIndexExporter ();
52
47
reEnableLocalIndexExporter ();
53
48
setLocalIndexToDebug ();
54
49
}
55
50
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
+ }
56
64
57
65
private void createDocument () throws IOException {
58
66
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 "
61
70
+ "}" ;
62
71
63
72
Request createDocumentRequest = new Request ("POST" , "/my-index-0/_doc/" );
@@ -66,103 +75,74 @@ private void createDocument() throws IOException {
66
75
assertEquals (201 , response .getStatusLine ().getStatusCode ());
67
76
}
68
77
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
-
83
78
private void checkLocalIndices (String context ) throws IOException {
84
79
Request indicesRequest = new Request ("GET" , "/_cat/indices?v" );
85
80
Response response = client ().performRequest (indicesRequest );
86
81
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-" ));
92
82
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-" ));
94
85
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 ());
95
90
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
+ }
96
107
97
108
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\" } }" ;
103
110
Request disableExporterRequest = new Request ("PUT" , "/_cluster/settings" );
104
111
disableExporterRequest .setJsonEntity (disableExporterJson );
105
- Response response = client ().performRequest (disableExporterRequest );
106
- assertEquals (200 , response .getStatusLine ().getStatusCode ());
112
+ client ().performRequest (disableExporterRequest );
107
113
}
108
114
109
115
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
- + "}" ;
115
116
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 );
119
119
}
120
120
121
121
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\" } }" ;
127
123
Request debugExporterRequest = new Request ("PUT" , "/_cluster/settings" );
128
124
debugExporterRequest .setJsonEntity (debugExporterJson );
129
- Response response = client ().performRequest (debugExporterRequest );
130
- assertEquals (200 , response .getStatusLine ().getStatusCode ());
125
+ client ().performRequest (debugExporterRequest );
131
126
}
132
127
133
128
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 + "\" } }" ;
139
130
Request windowSizeRequest = new Request ("PUT" , "/_cluster/settings" );
140
131
windowSizeRequest .setJsonEntity (windowSizeJson );
141
- Response response = client ().performRequest (windowSizeRequest );
142
- assertEquals (200 , response .getStatusLine ().getStatusCode ());
132
+ client ().performRequest (windowSizeRequest );
143
133
}
144
134
145
135
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 );
148
137
}
149
138
150
139
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\" } }" ;
156
141
}
157
142
158
143
private String [] invalidExporterSettings () {
159
144
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\" } }" };
167
147
}
168
148
}
0 commit comments