Skip to content

Commit

Permalink
add queryFieldNames field in Doc Level Queries
Browse files Browse the repository at this point in the history
  • Loading branch information
eirsep committed Jan 24, 2024
1 parent 195b17d commit e937440
Showing 1 changed file with 46 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ data class DocLevelQuery(
val name: String,
val fields: List<String>,
val query: String,
val tags: List<String> = mutableListOf()
val tags: List<String> = mutableListOf(),
val queryFieldNames: List<String> = mutableListOf(),
) : BaseModel {

init {
Expand All @@ -33,7 +34,8 @@ data class DocLevelQuery(
sin.readString(), // name
sin.readStringList(), // fields
sin.readString(), // query
sin.readStringList() // tags
sin.readStringList(), // tags,
sin.readStringList() // fieldsBeingQueried
)

fun asTemplateArg(): Map<String, Any> {
Expand All @@ -42,7 +44,8 @@ data class DocLevelQuery(
NAME_FIELD to name,
FIELDS_FIELD to fields,
QUERY_FIELD to query,
TAGS_FIELD to tags
TAGS_FIELD to tags,
QUERY_FIELD_NAMES_FIELD to queryFieldNames
)
}

Expand All @@ -53,6 +56,7 @@ data class DocLevelQuery(
out.writeStringCollection(fields)
out.writeString(query)
out.writeStringCollection(tags)
out.writeStringCollection(queryFieldNames)
}

override fun toXContent(builder: XContentBuilder, params: ToXContent.Params): XContentBuilder {
Expand All @@ -62,6 +66,7 @@ data class DocLevelQuery(
.field(FIELDS_FIELD, fields.toTypedArray())
.field(QUERY_FIELD, query)
.field(TAGS_FIELD, tags.toTypedArray())
.field(QUERY_FIELD_NAMES_FIELD, queryFieldNames.toTypedArray())
.endObject()
return builder
}
Expand All @@ -72,16 +77,19 @@ data class DocLevelQuery(
const val FIELDS_FIELD = "fields"
const val QUERY_FIELD = "query"
const val TAGS_FIELD = "tags"
const val QUERY_FIELD_NAMES_FIELD = "queru_field_names"
const val NO_ID = ""
val INVALID_CHARACTERS: List<String> = listOf(" ", "[", "]", "{", "}", "(", ")")

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun parse(xcp: XContentParser): DocLevelQuery {
var id: String = UUID.randomUUID().toString()
lateinit var query: String
lateinit var name: String
val tags: MutableList<String> = mutableListOf()
val fields: MutableList<String> = mutableListOf()
val queryFieldNames: MutableList<String> = mutableListOf()

XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, xcp.currentToken(), xcp)
while (xcp.nextToken() != XContentParser.Token.END_OBJECT) {
Expand All @@ -94,6 +102,7 @@ data class DocLevelQuery(
name = xcp.text()
validateQuery(name)
}

QUERY_FIELD -> query = xcp.text()
TAGS_FIELD -> {
XContentParserUtils.ensureExpectedToken(
Expand All @@ -107,6 +116,7 @@ data class DocLevelQuery(
tags.add(tag)
}
}

FIELDS_FIELD -> {
XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_ARRAY,
Expand All @@ -118,6 +128,18 @@ data class DocLevelQuery(
fields.add(field)
}
}

QUERY_FIELD_NAMES_FIELD -> {
XContentParserUtils.ensureExpectedToken(
XContentParser.Token.START_ARRAY,
xcp.currentToken(),
xcp
)
while (xcp.nextToken() != XContentParser.Token.END_ARRAY) {
val field = xcp.text()
queryFieldNames.add(field)

Check warning on line 140 in src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt#L139-L140

Added lines #L139 - L140 were not covered by tests
}
}
}
}

Expand All @@ -126,11 +148,13 @@ data class DocLevelQuery(
name = name,
fields = fields,
query = query,
tags = tags
tags = tags,
queryFieldNames = queryFieldNames
)
}

@JvmStatic @Throws(IOException::class)
@JvmStatic
@Throws(IOException::class)
fun readFrom(sin: StreamInput): DocLevelQuery {
return DocLevelQuery(sin)
}
Expand All @@ -146,4 +170,20 @@ data class DocLevelQuery(
}
}
}

// constructor for java plugins' convenience to optionally avoid passing empty list for 'fieldsBeingQueried' field
constructor(
id: String,
name: String,
fields: MutableList<String>,
query: String,
tags: MutableList<String>,
) : this(
id = id,
name = name,
fields = fields,
query = query,
tags = tags,
queryFieldNames = emptyList()
)

Check warning on line 188 in src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt

View check run for this annotation

Codecov / codecov/patch

src/main/kotlin/org/opensearch/commons/alerting/model/DocLevelQuery.kt#L181-L188

Added lines #L181 - L188 were not covered by tests
}

0 comments on commit e937440

Please sign in to comment.