From 5cbae4a6a8c924ae3ccaba25f845b59a8a197376 Mon Sep 17 00:00:00 2001 From: Petar Dzepina Date: Wed, 4 Jan 2023 01:56:17 +0100 Subject: [PATCH] fixed regarding fields Signed-off-by: Petar Dzepina --- .../rollup/query/QueryStringQueryParserExt.kt | 22 +++++++++++++------ .../rollup/query/QueryStringQueryUtil.kt | 16 +++++++++++--- .../rollup/RollupRestTestCase.kt | 6 ++--- .../rollup/interceptor/RollupInterceptorIT.kt | 14 ++++++------ 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryParserExt.kt b/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryParserExt.kt index 4f93d187c..dcb39b719 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryParserExt.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryParserExt.kt @@ -12,37 +12,45 @@ import org.opensearch.index.search.QueryStringQueryParser class QueryStringQueryParserExt : QueryStringQueryParser { val discoveredFields = mutableListOf() + var hasLonelyTerms = false constructor(context: QueryShardContext?, lenient: Boolean) : super(context, lenient) constructor(context: QueryShardContext?, defaultField: String, lenient: Boolean) : super(context, defaultField, lenient) constructor(context: QueryShardContext, resolvedFields: Map, lenient: Boolean) : super(context, resolvedFields, lenient) override fun getFuzzyQuery(field: String?, termStr: String?, minSimilarity: Float): Query? { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getFuzzyQuery(field, termStr, minSimilarity) } override fun getPrefixQuery(field: String?, termStr: String?): Query { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getPrefixQuery(field, termStr) } override fun getFieldQuery(field: String?, queryText: String?, quoted: Boolean): Query { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getFieldQuery(field, queryText, quoted) } override fun getWildcardQuery(field: String?, termStr: String?): Query { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getWildcardQuery(field, termStr) } override fun getFieldQuery(field: String?, queryText: String?, slop: Int): Query { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getFieldQuery(field, queryText, slop) } override fun getRangeQuery(field: String?, part1: String?, part2: String?, startInclusive: Boolean, endInclusive: Boolean): Query { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getRangeQuery(field, part1, part2, startInclusive, endInclusive) } override fun getRegexpQuery(field: String?, termStr: String?): Query { - if (field != null && field != "*") discoveredFields.add(field) + if (field == null) hasLonelyTerms = true + else if (field != "*") discoveredFields.add(field) return super.getRegexpQuery(field, termStr) } } diff --git a/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryUtil.kt b/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryUtil.kt index 9638798db..639eef33d 100644 --- a/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryUtil.kt +++ b/src/main/kotlin/org/opensearch/indexmanagement/rollup/query/QueryStringQueryUtil.kt @@ -34,6 +34,14 @@ object QueryStringQueryUtil { fieldsFromQueryString.forEach { field -> newQueryString = newQueryString.replace("$field:", "$field.${Dimension.Type.TERMS.type}:") } + + var newDefaultField = qsqBuilder.defaultField() + if (newDefaultField != null && newDefaultField != "*") { + newDefaultField = newDefaultField + ".${Dimension.Type.TERMS.type}" + } else { + newDefaultField = null + } + var newFields: MutableMap? = null if (otherFields.isNotEmpty()) { newFields = mutableMapOf() @@ -64,7 +72,9 @@ object QueryStringQueryUtil { .boost(qsqBuilder.boost()) .fuzzyTranspositions(qsqBuilder.fuzzyTranspositions()) - if (newFields != null && newFields.size > 0) { + if (newDefaultField != null) { + retVal = retVal.defaultField(newDefaultField) + } else if (newFields != null && newFields.size > 0) { retVal = retVal.fields(newFields) } if (qsqBuilder.tieBreaker() != null) { @@ -153,10 +163,10 @@ object QueryStringQueryUtil { try { queryParser.parse(rewrittenQueryString) } catch (e: ParseException) { - throw QueryShardException(context, "Failed to parse query [" + qsqBuilder.queryString() + "]", e) + throw IllegalArgumentException("Failed to parse query [" + qsqBuilder.queryString() + "]", e) } // Return discovered fields - return queryParser.discoveredFields to otherFields + return queryParser.discoveredFields to if (queryParser.hasLonelyTerms) otherFields else mapOf() } @Suppress("EmptyCatchBlock", "LoopWithTooManyJumpStatements") diff --git a/src/test/kotlin/org/opensearch/indexmanagement/rollup/RollupRestTestCase.kt b/src/test/kotlin/org/opensearch/indexmanagement/rollup/RollupRestTestCase.kt index 3a0161c08..f21fa41a5 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/rollup/RollupRestTestCase.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/rollup/RollupRestTestCase.kt @@ -362,7 +362,7 @@ abstract class RollupRestTestCase : IndexManagementRestTestCase() { "test.vvv": "54321", "state": "TX", "state_ext": "CA", - "state_ordinal": ${random().nextInt(10)}, + "state_ordinal": ${i % 3}, "earnings": $i } """.trimIndent() @@ -374,7 +374,7 @@ abstract class RollupRestTestCase : IndexManagementRestTestCase() { "event_ts": "2019-01-01T12:10:30Z", "state": "TA", "state_ext": "SE", - "state_ordinal": ${random().nextInt(10)}, + "state_ordinal": ${i % 3}, "earnings": $i } """.trimIndent() @@ -386,7 +386,7 @@ abstract class RollupRestTestCase : IndexManagementRestTestCase() { "event_ts": "2019-01-02T12:10:30Z", "state": "CA", "state_ext": "MA", - "state_ordinal": ${random().nextInt(10)}, + "state_ordinal": ${i % 3}, "earnings": $i } """.trimIndent() diff --git a/src/test/kotlin/org/opensearch/indexmanagement/rollup/interceptor/RollupInterceptorIT.kt b/src/test/kotlin/org/opensearch/indexmanagement/rollup/interceptor/RollupInterceptorIT.kt index c47a81f0a..fbc471b1d 100644 --- a/src/test/kotlin/org/opensearch/indexmanagement/rollup/interceptor/RollupInterceptorIT.kt +++ b/src/test/kotlin/org/opensearch/indexmanagement/rollup/interceptor/RollupInterceptorIT.kt @@ -1085,7 +1085,7 @@ class RollupInterceptorIT : RollupRestTestCase() { createSampleIndexForQSQTest(sourceIndex) val rollup = Rollup( - id = "basic_query_string_query_rollup_search", + id = "basic_query_string_query_rollup_search111", enabled = true, schemaVersion = 1L, jobSchedule = IntervalSchedule(Instant.now(), 1, ChronoUnit.MINUTES), @@ -1133,7 +1133,7 @@ class RollupInterceptorIT : RollupRestTestCase() { "size": 0, "query": { "query_string": { - "query": "state:TX AND state_ext:CA AND 5", + "query": "state:TX AND state_ext:CA AND 0", "default_field": "state_ordinal" } @@ -1278,8 +1278,8 @@ class RollupInterceptorIT : RollupRestTestCase() { } fun `test roll up search query_string query with missing fields in fields and default_field`() { - val sourceIndex = "source_rollup_search_qsq_2" - val targetIndex = "target_rollup_qsq_search_2" + val sourceIndex = "source_rollup_search_qsq_22" + val targetIndex = "target_rollup_qsq_search_22" createSampleIndexForQSQTest(sourceIndex) @@ -1542,7 +1542,7 @@ class RollupInterceptorIT : RollupRestTestCase() { "size": 0, "query": { "query_string": { - "query": "invalid query" + "query": "::!invalid+-+-::query:::" } }, "aggs": { @@ -1558,7 +1558,7 @@ class RollupInterceptorIT : RollupRestTestCase() { client().makeRequest("POST", "/$targetIndex/_search", emptyMap(), StringEntity(req, ContentType.APPLICATION_JSON)) fail("search should've failed due to incorrect query") } catch (e: ResponseException) { - assertTrue("The query_string query wasn't invalid", e.message!!.contains("The query_string query is invalid")) + assertTrue("The query_string query wasn't invalid", e.message!!.contains("Failed to parse query")) } } @@ -1631,7 +1631,7 @@ class RollupInterceptorIT : RollupRestTestCase() { client().makeRequest("POST", "/$targetIndex/_search", emptyMap(), StringEntity(req, ContentType.APPLICATION_JSON)) fail("search should've failed due to incorrect query") } catch (e: ResponseException) { - assertTrue("The query_string query wasn't invalid", e.message!!.contains("The query_string query is invalid")) + assertTrue("The query_string query wasn't invalid", e.message!!.contains("[missing field unknown_field]")) } } }