From 8503c6d5a7d658ec869d0a52c907fd7ee4cfe92c Mon Sep 17 00:00:00 2001 From: Aurelien FOUCRET Date: Wed, 6 Nov 2024 16:33:04 +0100 Subject: [PATCH] Rename KqlParserExecutionContext into KqlParsingContext --- .../xpack/kql/parser/KqlAstBuilder.java | 26 +++++++++---------- .../xpack/kql/parser/KqlParser.java | 6 ++--- ...ionContext.java => KqlParsingContext.java} | 13 +++------- .../xpack/kql/query/KqlQueryBuilder.java | 6 ++--- .../kql/parser/AbstractKqlParserTestCase.java | 2 +- 5 files changed, 24 insertions(+), 29 deletions(-) rename x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/{KqlParserExecutionContext.java => KqlParsingContext.java} (91%) diff --git a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlAstBuilder.java b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlAstBuilder.java index db34c42ebf323..983dffa49dcc4 100644 --- a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlAstBuilder.java +++ b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlAstBuilder.java @@ -22,17 +22,17 @@ import java.util.function.BiFunction; import static org.elasticsearch.common.logging.LoggerMessageFormat.format; -import static org.elasticsearch.xpack.kql.parser.KqlParserExecutionContext.isDateField; -import static org.elasticsearch.xpack.kql.parser.KqlParserExecutionContext.isKeywordField; -import static org.elasticsearch.xpack.kql.parser.KqlParserExecutionContext.isRuntimeField; +import static org.elasticsearch.xpack.kql.parser.KqlParsingContext.isDateField; +import static org.elasticsearch.xpack.kql.parser.KqlParsingContext.isKeywordField; +import static org.elasticsearch.xpack.kql.parser.KqlParsingContext.isRuntimeField; import static org.elasticsearch.xpack.kql.parser.ParserUtils.escapeLuceneQueryString; import static org.elasticsearch.xpack.kql.parser.ParserUtils.hasWildcard; class KqlAstBuilder extends KqlBaseBaseVisitor { - private final KqlParserExecutionContext kqlParserExecutionContext; + private final KqlParsingContext kqlParsingContext; - KqlAstBuilder(KqlParserExecutionContext kqlParserExecutionContext) { - this.kqlParserExecutionContext = kqlParserExecutionContext; + KqlAstBuilder(KqlParsingContext kqlParsingContext) { + this.kqlParsingContext = kqlParsingContext; } public QueryBuilder toQueryBuilder(ParserRuleContext ctx) { @@ -127,8 +127,8 @@ public QueryBuilder visitRangeQuery(KqlBaseParser.RangeQueryContext ctx) { withFields(ctx.fieldName(), (fieldName, mappedFieldType) -> { RangeQueryBuilder rangeQuery = rangeOperation.apply(QueryBuilders.rangeQuery(fieldName), queryText); - if (kqlParserExecutionContext.timeZone() != null) { - rangeQuery.timeZone(kqlParserExecutionContext.timeZone().getId()); + if (kqlParsingContext.timeZone() != null) { + rangeQuery.timeZone(kqlParsingContext.timeZone().getId()); } boolQueryBuilder.should(rangeQuery); @@ -165,17 +165,17 @@ public QueryBuilder visitFieldQuery(KqlBaseParser.FieldQueryContext ctx) { QueryBuilder fieldQuery = null; if (hasWildcard && isKeywordField(mappedFieldType)) { - fieldQuery = QueryBuilders.wildcardQuery(fieldName, queryText).caseInsensitive(kqlParserExecutionContext.caseInsensitive()); + fieldQuery = QueryBuilders.wildcardQuery(fieldName, queryText).caseInsensitive(kqlParsingContext.caseInsensitive()); } else if (hasWildcard) { fieldQuery = QueryBuilders.queryStringQuery(escapeLuceneQueryString(queryText, true)).field(fieldName); } else if (isDateField(mappedFieldType)) { RangeQueryBuilder rangeFieldQuery = QueryBuilders.rangeQuery(fieldName).gte(queryText).lte(queryText); - if (kqlParserExecutionContext.timeZone() != null) { - rangeFieldQuery.timeZone(kqlParserExecutionContext.timeZone().getId()); + if (kqlParsingContext.timeZone() != null) { + rangeFieldQuery.timeZone(kqlParsingContext.timeZone().getId()); } fieldQuery = rangeFieldQuery; } else if (isKeywordField(mappedFieldType)) { - fieldQuery = QueryBuilders.termQuery(fieldName, queryText).caseInsensitive(kqlParserExecutionContext.caseInsensitive()); + fieldQuery = QueryBuilders.termQuery(fieldName, queryText).caseInsensitive(kqlParsingContext.caseInsensitive()); } else if (ctx.fieldQueryValue().QUOTED_STRING() != null) { fieldQuery = QueryBuilders.matchPhraseQuery(fieldName, queryText); } else { @@ -199,7 +199,7 @@ private static boolean isOrQuery(KqlBaseParser.BooleanQueryContext ctx) { } private void withFields(KqlBaseParser.FieldNameContext ctx, BiConsumer fieldConsummer) { - kqlParserExecutionContext.resolveFields(ctx).forEach(fieldDef -> fieldConsummer.accept(fieldDef.v1(), fieldDef.v2())); + kqlParsingContext.resolveFields(ctx).forEach(fieldDef -> fieldConsummer.accept(fieldDef.v1(), fieldDef.v2())); } private QueryBuilder rewriteDisjunctionQuery(BoolQueryBuilder boolQueryBuilder) { diff --git a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParser.java b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParser.java index 78e4784dc003e..ff0f28588d3b4 100644 --- a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParser.java +++ b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParser.java @@ -24,7 +24,7 @@ public class KqlParser { private static final Logger log = LogManager.getLogger(KqlParser.class); - public QueryBuilder parseKqlQuery(String kqlQuery, KqlParserExecutionContext kqlParserContext) { + public QueryBuilder parseKqlQuery(String kqlQuery, KqlParsingContext kqlParserContext) { if (log.isDebugEnabled()) { log.debug("Parsing KQL query: {}", kqlQuery); } @@ -34,7 +34,7 @@ public QueryBuilder parseKqlQuery(String kqlQuery, KqlParserExecutionContext kql private T invokeParser( String kqlQuery, - KqlParserExecutionContext kqlParserExecutionContext, + KqlParsingContext kqlParsingContext, Function parseFunction, BiFunction visitor ) { @@ -57,7 +57,7 @@ private T invokeParser( log.trace("Parse tree: {}", tree.toStringTree()); } - return visitor.apply(new KqlAstBuilder(kqlParserExecutionContext), tree); + return visitor.apply(new KqlAstBuilder(kqlParsingContext), tree); } private static final BaseErrorListener ERROR_LISTENER = new BaseErrorListener() { diff --git a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParserExecutionContext.java b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParsingContext.java similarity index 91% rename from x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParserExecutionContext.java rename to x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParsingContext.java index 420d66d059fe7..a281ce77f9d8d 100644 --- a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParserExecutionContext.java +++ b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/parser/KqlParsingContext.java @@ -22,7 +22,7 @@ import static org.elasticsearch.core.Tuple.tuple; -public class KqlParserExecutionContext { +public class KqlParsingContext { private static final List IGNORED_METADATA_FIELDS = List.of( "_seq_no", @@ -48,12 +48,7 @@ public static Builder builder(QueryRewriteContext queryRewriteContext) { private final String defaultFields; - public KqlParserExecutionContext( - QueryRewriteContext queryRewriteContext, - boolean caseInsensitive, - ZoneId timeZone, - String defaultFields - ) { + public KqlParsingContext(QueryRewriteContext queryRewriteContext, boolean caseInsensitive, ZoneId timeZone, String defaultFields) { this.queryRewriteContext = queryRewriteContext; this.caseInsensitive = caseInsensitive; this.timeZone = timeZone; @@ -109,8 +104,8 @@ private Builder(QueryRewriteContext queryRewriteContext) { this.queryRewriteContext = queryRewriteContext; } - public KqlParserExecutionContext build() { - return new KqlParserExecutionContext(queryRewriteContext, caseInsensitive, timeZone, defaultFields); + public KqlParsingContext build() { + return new KqlParsingContext(queryRewriteContext, caseInsensitive, timeZone, defaultFields); } public Builder caseInsensitive(boolean caseInsensitive) { diff --git a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/query/KqlQueryBuilder.java b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/query/KqlQueryBuilder.java index c349c27433ea6..cbcc50590cd3b 100644 --- a/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/query/KqlQueryBuilder.java +++ b/x-pack/plugin/kql/src/main/java/org/elasticsearch/xpack/kql/query/KqlQueryBuilder.java @@ -25,7 +25,7 @@ import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentParser; import org.elasticsearch.xpack.kql.parser.KqlParser; -import org.elasticsearch.xpack.kql.parser.KqlParserExecutionContext; +import org.elasticsearch.xpack.kql.parser.KqlParsingContext; import java.io.IOException; import java.time.ZoneId; @@ -191,8 +191,8 @@ protected boolean doEquals(KqlQueryBuilder other) { && caseInsensitive == other.caseInsensitive; } - private KqlParserExecutionContext createKqlParserContext(QueryRewriteContext queryRewriteContext) { - return KqlParserExecutionContext.builder(queryRewriteContext) + private KqlParsingContext createKqlParserContext(QueryRewriteContext queryRewriteContext) { + return KqlParsingContext.builder(queryRewriteContext) .caseInsensitive(caseInsensitive) .timeZone(timeZone) .defaultFields(defaultFields) diff --git a/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/AbstractKqlParserTestCase.java b/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/AbstractKqlParserTestCase.java index f3e375dce44fe..ac06a96d49eb4 100644 --- a/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/AbstractKqlParserTestCase.java +++ b/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/AbstractKqlParserTestCase.java @@ -110,7 +110,7 @@ protected List searchableFields(String fieldNamePattern) { protected QueryBuilder parseKqlQuery(String kqlQuery) { KqlParser parser = new KqlParser(); - KqlParserExecutionContext kqlParserContext = KqlParserExecutionContext.builder(createQueryRewriteContext()).build(); + KqlParsingContext kqlParserContext = KqlParsingContext.builder(createQueryRewriteContext()).build(); return parser.parseKqlQuery(kqlQuery, kqlParserContext); }