@@ -21,6 +21,7 @@ import org.opensearch.alerting.alerts.AlertIndices
21
21
import org.opensearch.alerting.core.ScheduledJobIndices
22
22
import org.opensearch.alerting.transport.AlertingSingleNodeTestCase
23
23
import org.opensearch.common.settings.Settings
24
+ import org.opensearch.common.xcontent.XContentType
24
25
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
25
26
import org.opensearch.commons.alerting.action.AlertingActions
26
27
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
@@ -141,8 +142,79 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
141
142
val docQuery3 = DocLevelQuery (query = " source.ip.v4.v0:120" , name = " 5" )
142
143
val docQuery4 = DocLevelQuery (query = " alias.some.fff:\" us-west-2\" " , name = " 6" )
143
144
val docQuery5 = DocLevelQuery (query = " message:\" This is an error from IAD region\" " , name = " 7" )
145
+ val docQuery6 = DocLevelQuery (query = " f1.type.f4:\" hello\" " , name = " 8" )
146
+ val docQuery7 = DocLevelQuery (query = " f1.type.f2.f3:\" world\" " , name = " 9" )
147
+ val docQuery8 = DocLevelQuery (query = " type:\" some type\" " , name = " 10" )
148
+
149
+ val docLevelInput = DocLevelMonitorInput (
150
+ " description" , listOf (index), listOf (docQuery1, docQuery2, docQuery3, docQuery4, docQuery5, docQuery6, docQuery7, docQuery8)
151
+ )
152
+ val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
153
+ val customFindingsIndex = " custom_findings_index"
154
+ val customFindingsIndexPattern = " custom_findings_index-1"
155
+ val customQueryIndex = " custom_alerts_index"
156
+ var monitor = randomDocumentLevelMonitor(
157
+ inputs = listOf (docLevelInput),
158
+ triggers = listOf (trigger),
159
+ dataSources = DataSources (
160
+ queryIndex = customQueryIndex,
161
+ findingsIndex = customFindingsIndex,
162
+ findingsIndexPattern = customFindingsIndexPattern
163
+ )
164
+ )
165
+ val monitorResponse = createMonitor(monitor)
166
+ // Trying to test here few different "nesting" situations and "wierd" characters
167
+ val testTime = DateTimeFormatter .ISO_OFFSET_DATE_TIME .format(ZonedDateTime .now().truncatedTo(MILLIS ))
168
+ val testDoc = """ {
169
+ "message" : "This is an error from IAD region",
170
+ "source.ip.v6.v1" : 12345,
171
+ "source.ip.v6.v2" : 16645,
172
+ "source.ip.v4.v0" : 120,
173
+ "test_bad_char" : "\u0000",
174
+ "test_strict_date_time" : "$testTime ",
175
+ "test_field.some_other_field" : "us-west-2",
176
+ "f1.type.f2.f3" : "world",
177
+ "f1.type.f4" : "hello",
178
+ "type" : "some type"
179
+ }"""
180
+ indexDoc(index, " 1" , testDoc)
181
+ client().admin().indices().putMapping(
182
+ PutMappingRequest (index).source(" alias.some.fff" , " type=alias,path=test_field.some_other_field" )
183
+ )
184
+ val mappings = " {\" properties\" :{\" type\" :{\" type\" :\" text\" ,\" fields\" :{\" keyword\" :{\" type\" :\" keyword\" ," +
185
+ " \" ignore_above\" :256}}},\" query\" :{\" type\" :\" text\" }}}"
186
+ val mappingsResp = client().admin().indices().putMapping(
187
+ PutMappingRequest (index).source(mappings, XContentType .JSON )
188
+ ).get()
189
+ assertFalse(monitorResponse?.id.isNullOrEmpty())
190
+ monitor = monitorResponse!! .monitor
191
+ val id = monitorResponse.id
192
+ val executeMonitorResponse = executeMonitor(monitor, id, false )
193
+ Assert .assertEquals(executeMonitorResponse!! .monitorRunResult.monitorName, monitor.name)
194
+ Assert .assertEquals(executeMonitorResponse.monitorRunResult.triggerResults.size, 1 )
195
+ searchAlerts(id)
196
+ val table = Table (" asc" , " id" , null , 1 , 0 , " " )
197
+ var getAlertsResponse = client()
198
+ .execute(AlertingActions .GET_ALERTS_ACTION_TYPE , GetAlertsRequest (table, " ALL" , " ALL" , null , null ))
199
+ .get()
200
+ Assert .assertTrue(getAlertsResponse != null )
201
+ Assert .assertTrue(getAlertsResponse.alerts.size == 1 )
202
+ val findings = searchFindings(id, customFindingsIndex)
203
+ assertEquals(" Findings saved for test monitor" , 1 , findings.size)
204
+ assertTrue(" Findings saved for test monitor" , findings[0 ].relatedDocIds.contains(" 1" ))
205
+ assertEquals(" Didn't match all 8 queries" , 8 , findings[0 ].docLevelQueries.size)
206
+ }
207
+
208
+ fun `test execute monitor with custom query index old` () {
209
+ val docQuery1 = DocLevelQuery (query = " source.ip.v6.v1:12345" , name = " 3" )
210
+ val docQuery2 = DocLevelQuery (query = " source.ip.v6.v2:16645" , name = " 4" )
211
+ val docQuery3 = DocLevelQuery (query = " source.ip.v4.v0:120" , name = " 5" )
212
+ val docQuery4 = DocLevelQuery (query = " alias.some.fff:\" us-west-2\" " , name = " 6" )
213
+ val docQuery5 = DocLevelQuery (query = " message:\" This is an error from IAD region\" " , name = " 7" )
214
+ val docQuery6 = DocLevelQuery (query = " type.subtype:\" some subtype\" " , name = " 8" )
215
+ val docQuery7 = DocLevelQuery (query = " supertype.type:\" some type\" " , name = " 9" )
144
216
val docLevelInput = DocLevelMonitorInput (
145
- " description" , listOf (index), listOf (docQuery1, docQuery2, docQuery3, docQuery4, docQuery5)
217
+ " description" , listOf (index), listOf (docQuery1, docQuery2, docQuery3, docQuery4, docQuery5, docQuery6, docQuery7 )
146
218
)
147
219
val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN )
148
220
val customFindingsIndex = " custom_findings_index"
@@ -167,7 +239,9 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
167
239
"source.ip.v4.v0" : 120,
168
240
"test_bad_char" : "\u0000",
169
241
"test_strict_date_time" : "$testTime ",
170
- "test_field.some_other_field" : "us-west-2"
242
+ "test_field.some_other_field" : "us-west-2",
243
+ "type.subtype" : "some subtype",
244
+ "supertype.type" : "some type"
171
245
}"""
172
246
indexDoc(index, " 1" , testDoc)
173
247
client().admin().indices().putMapping(
@@ -190,7 +264,7 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
190
264
val findings = searchFindings(id, customFindingsIndex)
191
265
assertEquals(" Findings saved for test monitor" , 1 , findings.size)
192
266
assertTrue(" Findings saved for test monitor" , findings[0 ].relatedDocIds.contains(" 1" ))
193
- assertEquals(" Didn't match all 5 queries" , 5 , findings[0 ].docLevelQueries.size)
267
+ assertEquals(" Didn't match all 7 queries" , 7 , findings[0 ].docLevelQueries.size)
194
268
}
195
269
196
270
fun `test execute monitor with custom query index and nested mappings` () {
0 commit comments