diff --git a/elasticmagic/api/elasticmagic.api b/elasticmagic/api/elasticmagic.api index 6a617635d9..aacfd89dfe 100644 --- a/elasticmagic/api/elasticmagic.api +++ b/elasticmagic/api/elasticmagic.api @@ -2039,7 +2039,7 @@ public abstract class dev/evo/elasticmagic/compile/BaseSearchQueryCompiler : dev public fun dispatch (Ldev/evo/elasticmagic/serde/Serializer$ArrayCtx;Ljava/lang/Object;)V public fun dispatch (Ldev/evo/elasticmagic/serde/Serializer$ObjectCtx;Ljava/lang/String;Ljava/lang/Object;)V public final fun visit (Ldev/evo/elasticmagic/serde/Serializer$ArrayCtx;Ldev/evo/elasticmagic/query/Expression;)V - public final fun visit (Ldev/evo/elasticmagic/serde/Serializer$ObjectCtx;Ldev/evo/elasticmagic/PreparedSearchQuery;Z)V + public fun visit (Ldev/evo/elasticmagic/serde/Serializer$ObjectCtx;Ldev/evo/elasticmagic/PreparedSearchQuery;)V public final fun visit (Ldev/evo/elasticmagic/serde/Serializer$ObjectCtx;Ldev/evo/elasticmagic/query/Expression;)V } @@ -2167,6 +2167,7 @@ public class dev/evo/elasticmagic/compile/SearchQueryCompiler : dev/evo/elasticm public final fun compile (Ldev/evo/elasticmagic/serde/Serde;Ldev/evo/elasticmagic/PreparedSearchQuery;Ljava/lang/String;)Ldev/evo/elasticmagic/transport/ApiRequest; public final fun compile (Ldev/evo/elasticmagic/serde/Serde;Ldev/evo/elasticmagic/SearchQueryWithIndex;)Ldev/evo/elasticmagic/transport/ApiRequest; public final fun processResult (Ldev/evo/elasticmagic/serde/Deserializer$ObjectCtx;Ldev/evo/elasticmagic/PreparedSearchQuery;)Ldev/evo/elasticmagic/SearchQueryResult; + public fun visit (Ldev/evo/elasticmagic/serde/Serializer$ObjectCtx;Ldev/evo/elasticmagic/PreparedSearchQuery;)V } public final class dev/evo/elasticmagic/compile/UpdateMappingCompiler : dev/evo/elasticmagic/compile/BaseCompiler { diff --git a/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/compile/SearchQueryCompiler.kt b/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/compile/SearchQueryCompiler.kt index 5cb8c89480..fd2c15866c 100644 --- a/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/compile/SearchQueryCompiler.kt +++ b/elasticmagic/src/commonMain/kotlin/dev/evo/elasticmagic/compile/SearchQueryCompiler.kt @@ -36,8 +36,7 @@ abstract class BaseSearchQueryCompiler( fun accept(ctx: T, compiler: BaseSearchQueryCompiler) } - @Suppress("ComplexMethod") - fun visit(ctx: ObjectCtx, searchQuery: PreparedSearchQuery<*>, isCountOnly: Boolean) { + open fun visit(ctx: ObjectCtx, searchQuery: PreparedSearchQuery<*>) { val query = searchQuery.query?.reduce() val filteredQuery = if (searchQuery.filters.isNotEmpty()) { if (query != null) { @@ -63,46 +62,97 @@ abstract class BaseSearchQueryCompiler( visit(this, postFilterExpr) } } - if (!isCountOnly && searchQuery.aggregations.isNotEmpty()) { + if (searchQuery.terminateAfter != null) { + ctx.field("terminate_after", searchQuery.terminateAfter) + } + } + + fun visit(ctx: ObjectCtx, expression: Expression) { + expression.accept(ctx, this) + } + + fun visit(ctx: ArrayCtx, expression: Expression) { + expression.accept(ctx, this) + } + + override fun dispatch(ctx: ArrayCtx, value: Any?) { + when (value) { + is ObjExpression -> ctx.obj { + visit(this, value) + } + is ArrayExpression -> { + visit(ctx, value) + } + is ToValue<*> -> { + ctx.value(value.toValue()) + } + else -> super.dispatch(ctx, value) + } + } + + override fun dispatch(ctx: ObjectCtx, name: String, value: Any?) { + when (value) { + is ObjExpression -> ctx.obj(name) { + visit(this, value) + } + is ArrayExpression -> ctx.array(name) { + visit(this, value) + } + is ToValue<*> -> { + ctx.field(name, value.toValue()) + } + else -> super.dispatch(ctx, name, value) + } + } +} + +open class SearchQueryCompiler( + features: ElasticsearchFeatures, +) : BaseSearchQueryCompiler(features) { + + @Suppress("ComplexMethod") + override fun visit(ctx: ObjectCtx, searchQuery: PreparedSearchQuery<*>) { + super.visit(ctx, searchQuery) + if (searchQuery.aggregations.isNotEmpty()) { ctx.obj("aggs") { visit(this, searchQuery.aggregations) } } - if (!isCountOnly && searchQuery.rescores.isNotEmpty()) { + if (searchQuery.rescores.isNotEmpty()) { ctx.array("rescore") { visit(this, searchQuery.rescores) } } - if (!isCountOnly && searchQuery.sorts.isNotEmpty()) { + if (searchQuery.sorts.isNotEmpty()) { ctx.array("sort") { visit(this, searchQuery.sorts) } } - if (!isCountOnly && searchQuery.trackScores != null) { + if (searchQuery.trackScores != null) { ctx.field("track_scores", searchQuery.trackScores) } - if (!isCountOnly && searchQuery.trackTotalHits != null && features.supportsTrackingOfTotalHits) { + if (searchQuery.trackTotalHits != null && features.supportsTrackingOfTotalHits) { ctx.field("track_total_hits", searchQuery.trackTotalHits) } if (searchQuery.source != null) { visit(ctx, searchQuery.source) } - if (!isCountOnly && searchQuery.fields.isNotEmpty()) { + if (searchQuery.fields.isNotEmpty()) { ctx.array("fields") { visit(this, searchQuery.fields) } } - if (!isCountOnly && searchQuery.docvalueFields.isNotEmpty()) { + if (searchQuery.docvalueFields.isNotEmpty()) { ctx.array("docvalue_fields") { visit(this, searchQuery.docvalueFields) } } - if (!isCountOnly && searchQuery.storedFields.isNotEmpty()) { + if (searchQuery.storedFields.isNotEmpty()) { ctx.array("stored_fields") { visit(this, searchQuery.storedFields) } } - if (!isCountOnly && searchQuery.scriptFields.isNotEmpty()) { + if (searchQuery.scriptFields.isNotEmpty()) { ctx.obj("script_fields") { for ((fieldName, script) in searchQuery.scriptFields) { obj(fieldName) { @@ -113,16 +163,13 @@ abstract class BaseSearchQueryCompiler( } } } - if (!isCountOnly && searchQuery.size != null) { + if (searchQuery.size != null) { ctx.field("size", searchQuery.size) } - if (!isCountOnly && searchQuery.from != null) { + if (searchQuery.from != null) { ctx.field("from", searchQuery.from) } - if (searchQuery.terminateAfter != null) { - ctx.field("terminate_after", searchQuery.terminateAfter) - } - if (!isCountOnly && searchQuery.extensions.isNotEmpty()) { + if (searchQuery.extensions.isNotEmpty()) { ctx.obj("ext") { for (ext in searchQuery.extensions) { visit(this, ext) @@ -131,53 +178,11 @@ abstract class BaseSearchQueryCompiler( } } - fun visit(ctx: ObjectCtx, expression: Expression) { - expression.accept(ctx, this) - } - - fun visit(ctx: ArrayCtx, expression: Expression) { - expression.accept(ctx, this) - } - - override fun dispatch(ctx: ArrayCtx, value: Any?) { - when (value) { - is ObjExpression -> ctx.obj { - visit(this, value) - } - is ArrayExpression -> { - visit(ctx, value) - } - is ToValue<*> -> { - ctx.value(value.toValue()) - } - else -> super.dispatch(ctx, value) - } - } - - override fun dispatch(ctx: ObjectCtx, name: String, value: Any?) { - when (value) { - is ObjExpression -> ctx.obj(name) { - visit(this, value) - } - is ArrayExpression -> ctx.array(name) { - visit(this, value) - } - is ToValue<*> -> { - ctx.field(name, value.toValue()) - } - else -> super.dispatch(ctx, name, value) - } - } -} - -open class SearchQueryCompiler( - features: ElasticsearchFeatures, -) : BaseSearchQueryCompiler(features) { fun compile( serde: Serde, input: PreparedSearchQuery, indexName: String ): ApiRequest> { val body = serde.serializer.obj { - visit(this, input, isCountOnly = false) + visit(this, input) } return ApiRequest( method = Method.POST, @@ -285,7 +290,7 @@ class CountQueryCompiler( serde: Serde, input: PreparedSearchQuery<*>, indexName: String ): ApiRequest { val body = serde.serializer.obj { - visit(this, input, isCountOnly = true) + visit(this, input) } return ApiRequest( method = Method.POST, @@ -310,6 +315,7 @@ class MultiSearchQueryCompiler( features: ElasticsearchFeatures, private val searchQueryCompiler: SearchQueryCompiler ) : BaseCompiler(features) { + fun compile( serde: Serde.OneLineJson, input: List> ): BulkRequest {