Skip to content

Commit

Permalink
Use the keyword managed_index.name subfield instead of the tokenized …
Browse files Browse the repository at this point in the history
…text field for querying
  • Loading branch information
dbbaughe committed Feb 16, 2022
1 parent c5d1739 commit 6d11226
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 2 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 @@ -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 @@ -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 6d11226

Please sign in to comment.