Skip to content

Commit

Permalink
Use dispatching of visit method for search query compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
anti-social committed Mar 18, 2023
1 parent 988f281 commit 33675a9
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 62 deletions.
3 changes: 2 additions & 1 deletion elasticmagic/api/elasticmagic.api
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<ObjectCtx>) {
expression.accept(ctx, this)
}

fun visit(ctx: ArrayCtx, expression: Expression<ArrayCtx>) {
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) {
Expand All @@ -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)
Expand All @@ -131,53 +178,11 @@ abstract class BaseSearchQueryCompiler(
}
}

fun visit(ctx: ObjectCtx, expression: Expression<ObjectCtx>) {
expression.accept(ctx, this)
}

fun visit(ctx: ArrayCtx, expression: Expression<ArrayCtx>) {
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 <S: BaseDocSource> compile(
serde: Serde, input: PreparedSearchQuery<S>, indexName: String
): ApiRequest<SearchQueryResult<S>> {
val body = serde.serializer.obj {
visit(this, input, isCountOnly = false)
visit(this, input)
}
return ApiRequest(
method = Method.POST,
Expand Down Expand Up @@ -285,7 +290,7 @@ class CountQueryCompiler(
serde: Serde, input: PreparedSearchQuery<*>, indexName: String
): ApiRequest<CountResult> {
val body = serde.serializer.obj {
visit(this, input, isCountOnly = true)
visit(this, input)
}
return ApiRequest(
method = Method.POST,
Expand All @@ -310,6 +315,7 @@ class MultiSearchQueryCompiler(
features: ElasticsearchFeatures,
private val searchQueryCompiler: SearchQueryCompiler
) : BaseCompiler(features) {

fun compile(
serde: Serde.OneLineJson, input: List<SearchQueryWithIndex<*>>
): BulkRequest<MultiSearchQueryResult> {
Expand Down

0 comments on commit 33675a9

Please sign in to comment.