Skip to content

Commit d239d00

Browse files
opensearch-trigger-bot[bot]github-actions[bot]
authored andcommitted
add queryFieldNames field in Doc Level Queries (opensearch-project#582) (opensearch-project#597)
* add queryFieldNames field in Doc Level Queries * add tests to verify queryFieldNames field in DocLevelQuery --------- (cherry picked from commit 75925dc) Signed-off-by: Surya Sashank Nistala <snistala@amazon.com> Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Signed-off-by: AWSHurneyt <hurneyt@amazon.com>
1 parent db98f57 commit d239d00

File tree

3 files changed

+68
-4
lines changed

3 files changed

+68
-4
lines changed

src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt

Lines changed: 42 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,8 @@ data class DocLevelQuery(
1616
val name: String,
1717
val fields: List<String>,
1818
val query: String,
19-
val tags: List<String> = mutableListOf()
19+
val tags: List<String> = mutableListOf(),
20+
val queryFieldNames: List<String> = mutableListOf()
2021
) : BaseModel {
2122

2223
init {
@@ -33,7 +34,8 @@ data class DocLevelQuery(
3334
sin.readString(), // name
3435
sin.readStringList(), // fields
3536
sin.readString(), // query
36-
sin.readStringList() // tags
37+
sin.readStringList(), // tags,
38+
sin.readStringList() // fieldsBeingQueried
3739
)
3840

3941
fun asTemplateArg(): Map<String, Any> {
@@ -42,7 +44,8 @@ data class DocLevelQuery(
4244
NAME_FIELD to name,
4345
FIELDS_FIELD to fields,
4446
QUERY_FIELD to query,
45-
TAGS_FIELD to tags
47+
TAGS_FIELD to tags,
48+
QUERY_FIELD_NAMES_FIELD to queryFieldNames
4649
)
4750
}
4851

@@ -53,6 +56,7 @@ data class DocLevelQuery(
5356
out.writeStringCollection(fields)
5457
out.writeString(query)
5558
out.writeStringCollection(tags)
59+
out.writeStringCollection(queryFieldNames)
5660
}
5761

5862
override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
@@ -62,6 +66,7 @@ data class DocLevelQuery(
6266
.field(FIELDS_FIELD, fields.toTypedArray())
6367
.field(QUERY_FIELD, query)
6468
.field(TAGS_FIELD, tags.toTypedArray())
69+
.field(QUERY_FIELD_NAMES_FIELD, queryFieldNames.toTypedArray())
6570
.endObject()
6671
return builder
6772
}
@@ -72,6 +77,7 @@ data class DocLevelQuery(
7277
const val FIELDS_FIELD = "fields"
7378
const val QUERY_FIELD = "query"
7479
const val TAGS_FIELD = "tags"
80+
const val QUERY_FIELD_NAMES_FIELD = "query_field_names"
7581
const val NO_ID = ""
7682
val INVALID_CHARACTERS: List<String> = listOf(" ", "[", "]", "{", "}", "(", ")")
7783

@@ -83,6 +89,7 @@ data class DocLevelQuery(
8389
lateinit var name: String
8490
val tags: MutableList<String> = mutableListOf()
8591
val fields: MutableList<String> = mutableListOf()
92+
val queryFieldNames: MutableList<String> = mutableListOf()
8693

8794
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
8895
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
@@ -95,6 +102,7 @@ data class DocLevelQuery(
95102
name = xcp.text()
96103
validateQuery(name)
97104
}
105+
98106
QUERY_FIELD -> query = xcp.text()
99107
TAGS_FIELD -> {
100108
XContentParserUtils.ensureExpectedToken(
@@ -108,6 +116,7 @@ data class DocLevelQuery(
108116
tags.add(tag)
109117
}
110118
}
119+
111120
FIELDS_FIELD -> {
112121
XContentParserUtils.ensureExpectedToken(
113122
XContentParser.Token.START_ARRAY,
@@ -119,6 +128,18 @@ data class DocLevelQuery(
119128
fields.add(field)
120129
}
121130
}
131+
132+
QUERY_FIELD_NAMES_FIELD -> {
133+
XContentParserUtils.ensureExpectedToken(
134+
XContentParser.Token.START_ARRAY,
135+
xcp.currentToken(),
136+
xcp
137+
)
138+
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) {
139+
val field = xcp.text()
140+
queryFieldNames.add(field)
141+
}
142+
}
122143
}
123144
}
124145

@@ -127,7 +148,8 @@ data class DocLevelQuery(
127148
name = name,
128149
fields = fields,
129150
query = query,
130-
tags = tags
151+
tags = tags,
152+
queryFieldNames = queryFieldNames
131153
)
132154
}
133155

@@ -148,4 +170,20 @@ data class DocLevelQuery(
148170
}
149171
}
150172
}
173+
174+
// constructor for java plugins' convenience to optionally avoid passing empty list for 'fieldsBeingQueried' field
175+
constructor(
176+
id: String,
177+
name: String,
178+
fields: MutableList<String>,
179+
query: String,
180+
tags: MutableList<String>
181+
) : this(
182+
id = id,
183+
name = name,
184+
fields = fields,
185+
query = query,
186+
tags = tags,
187+
queryFieldNames = emptyList()
188+
)
151189
}

src/test/kotlin/org/opensearch/commons/alerting/model/WriteableTests.kt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import org.opensearch.commons.alerting.randomUserEmpty
2020
import org.opensearch.commons.authuser.User
2121
import org.opensearch.core.common.io.stream.StreamInput
2222
import org.opensearch.search.builder.SearchSourceBuilder
23+
import kotlin.test.assertTrue
2324

2425
class WriteableTests {
2526

@@ -121,6 +122,19 @@ class WriteableTests {
121122
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
122123
val newDlq = DocLevelQuery.readFrom(sin)
123124
Assertions.assertEquals(dlq, newDlq, "Round tripping DocLevelQuery doesn't work")
125+
assertTrue(newDlq.queryFieldNames.isEmpty())
126+
}
127+
128+
@Test
129+
fun `test doc-level query with query Field Names as stream`() {
130+
val dlq = randomDocLevelQuery().copy(queryFieldNames = listOf("f1", "f2"))
131+
val out = BytesStreamOutput()
132+
dlq.writeTo(out)
133+
val sin = StreamInput.wrap(out.bytes().toBytesRef().bytes)
134+
val newDlq = DocLevelQuery.readFrom(sin)
135+
assertTrue(newDlq.queryFieldNames.contains(dlq.queryFieldNames[0]))
136+
assertTrue(newDlq.queryFieldNames.contains(dlq.queryFieldNames[1]))
137+
Assertions.assertEquals(dlq, newDlq, "Round tripping DocLevelQuery doesn't work")
124138
}
125139

126140
@Test

src/test/kotlin/org/opensearch/commons/alerting/model/XContentTests.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,18 @@ class XContentTests {
431431
)
432432
}
433433

434+
@Test
435+
fun `test doc level query toXcontent with query field names`() {
436+
val dlq = DocLevelQuery("id", "name", listOf("f1", "f2"), "query", listOf("t1", "t2"), listOf("f1", "f2"))
437+
val dlqString = dlq.toXContent(builder(), ToXContent.EMPTY_PARAMS).string()
438+
val parsedDlq = DocLevelQuery.parse(parser(dlqString))
439+
Assertions.assertEquals(
440+
dlq,
441+
parsedDlq,
442+
"Round tripping Doc level query doesn't work"
443+
)
444+
}
445+
434446
@Test
435447
fun `test alert parsing`() {
436448
val alert = randomAlert()

0 commit comments

Comments
 (0)