Skip to content

Commit daad6b1

Browse files
raj-chakgithub-actions[bot]
authored andcommitted
fix percolator mapping error when having field name 'type' (#726)
Signed-off-by: Raj Chakravarthi <[email protected]> (cherry picked from commit 1098203)
1 parent bc108d4 commit daad6b1

File tree

2 files changed

+79
-5
lines changed

2 files changed

+79
-5
lines changed

alerting/src/main/kotlin/org/opensearch/alerting/util/DocLevelMonitorQueries.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -153,10 +153,10 @@ class DocLevelMonitorQueries(private val client: Client, private val clusterServ
153153
flattenPaths: MutableList<String>
154154
) {
155155
// If node contains "properties" property then it is internal(non-leaf) node
156+
log.debug("Node in traverse: $node")
156157
if (node.containsKey(PROPERTIES)) {
157158
return traverseMappingsAndUpdate(node.get(PROPERTIES) as MutableMap<String, Any>, currentPath, processLeafFn, flattenPaths)
158-
} else if (node.containsKey(TYPE) == false) {
159-
// If there is no "type" property, this is either internal(non-leaf) node or leaf node
159+
} else {
160160
// newNodes will hold list of updated leaf properties
161161
var newNodes = ArrayList<Triple<String, String, Any>>(node.size)
162162
node.entries.forEach {

alerting/src/test/kotlin/org/opensearch/alerting/MonitorDataSourcesIT.kt

Lines changed: 77 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import org.opensearch.alerting.alerts.AlertIndices
2121
import org.opensearch.alerting.core.ScheduledJobIndices
2222
import org.opensearch.alerting.transport.AlertingSingleNodeTestCase
2323
import org.opensearch.common.settings.Settings
24+
import org.opensearch.common.xcontent.XContentType
2425
import org.opensearch.commons.alerting.action.AcknowledgeAlertRequest
2526
import org.opensearch.commons.alerting.action.AlertingActions
2627
import org.opensearch.commons.alerting.action.DeleteMonitorRequest
@@ -141,8 +142,79 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
141142
val docQuery3 = DocLevelQuery(query = "source.ip.v4.v0:120", name = "5")
142143
val docQuery4 = DocLevelQuery(query = "alias.some.fff:\"us-west-2\"", name = "6")
143144
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")
144216
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)
146218
)
147219
val trigger = randomDocumentLevelTrigger(condition = ALWAYS_RUN)
148220
val customFindingsIndex = "custom_findings_index"
@@ -167,7 +239,9 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
167239
"source.ip.v4.v0" : 120,
168240
"test_bad_char" : "\u0000",
169241
"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"
171245
}"""
172246
indexDoc(index, "1", testDoc)
173247
client().admin().indices().putMapping(
@@ -190,7 +264,7 @@ class MonitorDataSourcesIT : AlertingSingleNodeTestCase() {
190264
val findings = searchFindings(id, customFindingsIndex)
191265
assertEquals("Findings saved for test monitor", 1, findings.size)
192266
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)
194268
}
195269

196270
fun `test execute monitor with custom query index and nested mappings`() {

0 commit comments

Comments
 (0)