diff --git a/wow-elasticsearch/src/main/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverter.kt b/wow-elasticsearch/src/main/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverter.kt index f464d8e4a3..c88868c561 100644 --- a/wow-elasticsearch/src/main/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverter.kt +++ b/wow-elasticsearch/src/main/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverter.kt @@ -23,6 +23,8 @@ import me.ahoo.wow.api.query.Condition import me.ahoo.wow.query.converter.AbstractConditionConverter import me.ahoo.wow.serialization.MessageRecords import me.ahoo.wow.serialization.state.StateAggregateRecords +import me.ahoo.wow.serialization.toJsonString +import java.io.StringReader import java.time.LocalDateTime import java.time.ZoneOffset @@ -237,11 +239,23 @@ object ElasticsearchConditionConverter : AbstractConditionConverter() { } override fun raw(condition: Condition): Query { - require(condition.value is Query) { - "raw condition value must be a Query." + return when (condition.value) { + is Query -> { + condition.valueAs() + } + + is String -> { + condition.valueAs().toQuery() + } + + else -> { + condition.value.toJsonString().toQuery() + } } + } - return condition.valueAs() + private fun String.toQuery(): Query { + return Query.Builder().withJson(StringReader(this)).build() } fun Condition.toQuery(): Query { diff --git a/wow-elasticsearch/src/test/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverterTest.kt b/wow-elasticsearch/src/test/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverterTest.kt index 76f7ece712..907cfaaf01 100644 --- a/wow-elasticsearch/src/test/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverterTest.kt +++ b/wow-elasticsearch/src/test/kotlin/me/ahoo/wow/elasticsearch/query/ElasticsearchConditionConverterTest.kt @@ -274,12 +274,18 @@ class ElasticsearchConditionConverterTest { } @Test - fun `raw to query - should throw exception when raw query is String`() { - val exception = assertThrows { - condition { - raw("") - }.toQuery() - } - assertThat(exception.message, equalTo("raw condition value must be a Query.")) + fun `string raw to query`() { + val query = condition { + raw("""{"match_all":{}}""") + }.toQuery() + assertThat(query._kind(), equalTo(Query.Kind.MatchAll)) + } + + @Test + fun `map raw to query`() { + val query = condition { + raw(mapOf("match_all" to emptyMap())) + }.toQuery() + assertThat(query._kind(), equalTo(Query.Kind.MatchAll)) } }