Skip to content

Commit

Permalink
Use the keyword subfield instead of the tokenized text field for quer…
Browse files Browse the repository at this point in the history
…ying managed indices and policies

Signed-off-by: Drew Baugher <[email protected]>
  • Loading branch information
dbbaughe committed Feb 16, 2022
1 parent c5d1739 commit 6a14f13
Show file tree
Hide file tree
Showing 5 changed files with 173 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class TransportExplainAction @Inject constructor(
.must(
QueryBuilders
.queryStringQuery(params.queryString)
.defaultField("managed_index.name")
.defaultField("managed_index.name.keyword")
.defaultOperator(Operator.AND)
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class TransportGetPoliciesAction @Inject constructor(
QueryBuilders
.queryStringQuery(params.queryString)
.defaultOperator(Operator.AND)
.field("policy.policy_id")
.field("policy.policy_id.keyword")
)

val searchSourceBuilder = SearchSourceBuilder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,29 @@ abstract class IndexStateManagementRestTestCase : IndexManagementRestTestCase()
return index to policyID
}

protected fun createDataStream(
dataStream: String,
template: StringEntity? = null
) {
val dataStreamTemplate = template ?: StringEntity(
"""
{
"data_stream": {},
"index_patterns": ["$dataStream"]
}
""".trimIndent(),
APPLICATION_JSON
)
val res = client().makeRequest(
"PUT",
"/_index_template/transform-data-stream-template",
dataStreamTemplate
)
assertEquals("Unexpected RestStatus", RestStatus.OK, res.restStatus())
val response = client().makeRequest("PUT", "/_data_stream/$dataStream")
assertEquals("Unexpected RestStatus", RestStatus.OK, response.restStatus())
}

protected fun changeAlias(
index: String,
alias: String,
Expand Down Expand Up @@ -532,9 +555,10 @@ abstract class IndexStateManagementRestTestCase : IndexManagementRestTestCase()
protected fun getFlatSettings(indexName: String) =
(getIndexSettings(indexName) as Map<String, Map<String, Map<String, Any?>>>)[indexName]!!["settings"] as Map<String, String>

protected fun getExplainMap(indexName: String?): Map<String, Any> {
protected fun getExplainMap(indexName: String?, queryParams: String = ""): Map<String, Any> {
var endpoint = RestExplainAction.EXPLAIN_BASE_URI
if (indexName != null) endpoint += "/$indexName"
if (queryParams.isNotEmpty()) endpoint += "?$queryParams"
val response = client().makeRequest(RestRequest.Method.GET.toString(), endpoint)
assertEquals("Unexpected RestStatus", RestStatus.OK, response.restStatus())
return response.asMap()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,4 +316,42 @@ class IndexStateManagementRestApiIT : IndexStateManagementRestTestCase() {

assertEquals(expectedMessage.toString(), actualMessage.toString())
}

fun `test get policies with hyphen`() {
val randomPolicy = randomPolicy(id = "testing-hyphens-01")
createPolicy(randomPolicy, policyId = randomPolicy.id, refresh = true)
val policy = getPolicy(randomPolicy.id)

val response = client().makeRequest(RestRequest.Method.GET.toString(), "$POLICY_BASE_URI?queryString=*testing-hyphens*")

val actualMessage = response.asMap()
val expectedMessage = mapOf(
"total_policies" to 1,
"policies" to listOf(
mapOf(
_SEQ_NO to policy.seqNo,
_ID to policy.id,
_PRIMARY_TERM to policy.primaryTerm,
Policy.POLICY_TYPE to mapOf(
"schema_version" to policy.schemaVersion,
"policy_id" to policy.id,
"last_updated_time" to policy.lastUpdatedTime.toEpochMilli(),
"default_state" to policy.defaultState,
"ism_template" to null,
"description" to policy.description,
"error_notification" to policy.errorNotification,
"states" to policy.states.map {
mapOf(
"name" to it.name,
"transitions" to it.transitions,
"actions" to it.actions
)
}
)
)
)
)

assertEquals(expectedMessage.toString(), actualMessage.toString())
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,114 @@ class RestExplainActionIT : IndexStateManagementRestTestCase() {
}
}

fun `test search query string`() {
val indexName1 = "$testIndexName-search-query-string"
val indexName2 = "$indexName1-testing-2"
val indexName3 = "$indexName1-testing-3"
val indexName4 = "$indexName1-testing-4-2022-02-15"
val indexName5 = "$indexName1-testing-5-15-02-2022"
val dataStreamName = "$indexName1-data-stream"
createDataStream(dataStreamName)

val policy = createRandomPolicy()
createIndex(indexName1, policyID = policy.id)
createIndex(indexName2, policyID = policy.id)
createIndex(indexName3, null)
createIndex(indexName4, policyID = policy.id)
createIndex(indexName5, policyID = policy.id)
addPolicyToIndex(dataStreamName, policy.id)
val indexName1Map = indexName1 to mapOf<String, Any>(
explainResponseOpendistroPolicyIdSetting to policy.id,
explainResponseOpenSearchPolicyIdSetting to policy.id,
"index" to indexName1,
"index_uuid" to getUuid(indexName1),
"policy_id" to policy.id,
"enabled" to true
)
val indexName2Map = indexName2 to mapOf<String, Any>(
explainResponseOpendistroPolicyIdSetting to policy.id,
explainResponseOpenSearchPolicyIdSetting to policy.id,
"index" to indexName2,
"index_uuid" to getUuid(indexName2),
"policy_id" to policy.id,
"enabled" to true
)
val indexName4Map = indexName4 to mapOf<String, Any>(
explainResponseOpendistroPolicyIdSetting to policy.id,
explainResponseOpenSearchPolicyIdSetting to policy.id,
"index" to indexName4,
"index_uuid" to getUuid(indexName4),
"policy_id" to policy.id,
"enabled" to true
)
val indexName5Map = indexName5 to mapOf<String, Any>(
explainResponseOpendistroPolicyIdSetting to policy.id,
explainResponseOpenSearchPolicyIdSetting to policy.id,
"index" to indexName5,
"index_uuid" to getUuid(indexName5),
"policy_id" to policy.id,
"enabled" to true
)
val datastreamMap = ".ds-$dataStreamName-000001" to mapOf<String, Any>(
explainResponseOpendistroPolicyIdSetting to policy.id,
explainResponseOpenSearchPolicyIdSetting to policy.id,
"index" to ".ds-$dataStreamName-000001",
"index_uuid" to getUuid(".ds-$dataStreamName-000001"),
"policy_id" to policy.id,
"enabled" to true
)

waitFor {
val expected = mapOf(
indexName1Map,
indexName2Map,
indexName4Map,
indexName5Map,
"total_managed_indices" to 4
)
// These should match all non datastream managed indices
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=$testIndexName*"))
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=$testIndexName-*"))
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=$testIndexName-search-*"))
}

waitFor {
val expected = mapOf(
indexName1Map,
indexName2Map,
indexName4Map,
indexName5Map,
datastreamMap,
"total_managed_indices" to 5
)
// These should match all managed indices including datastreams
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=*$testIndexName-*"))
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=*search*"))
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=*search-query*"))
}

waitFor {
val expected = mapOf(
datastreamMap,
"total_managed_indices" to 1
)
// These should match all datastream managed indices (and system/hidden indices)
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=.*"))
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=.ds-$testIndexName-*"))
}

waitFor {
val expected = mapOf(
indexName4Map,
"total_managed_indices" to 1
)
// These should match all just the single index, and validates that it does not match the 15-02-2022 index
// i.e. if it was still matching on tokens then ["2022", "02", "15"] would match both which we don't want
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=*2022-02-15"))
assertResponseMap(expected, getExplainMap(indexName = null, queryParams = "queryString=*2022-02-15*"))
}
}

fun `test attached policy`() {
val indexName = "${testIndexName}_watermelon"
val policy = createRandomPolicy()
Expand Down

0 comments on commit 6a14f13

Please sign in to comment.