Skip to content

Commit

Permalink
Rename KqlParserExecutionContext into KqlParsingContext
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 6, 2024
1 parent 57a225b commit 8503c6d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<QueryBuilder> {
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) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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 {
Expand All @@ -199,7 +199,7 @@ private static boolean isOrQuery(KqlBaseParser.BooleanQueryContext ctx) {
}

private void withFields(KqlBaseParser.FieldNameContext ctx, BiConsumer<String, MappedFieldType> 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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand All @@ -34,7 +34,7 @@ public QueryBuilder parseKqlQuery(String kqlQuery, KqlParserExecutionContext kql

private <T> T invokeParser(
String kqlQuery,
KqlParserExecutionContext kqlParserExecutionContext,
KqlParsingContext kqlParsingContext,
Function<KqlBaseParser, ParserRuleContext> parseFunction,
BiFunction<KqlAstBuilder, ParserRuleContext, T> visitor
) {
Expand All @@ -57,7 +57,7 @@ private <T> 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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

import static org.elasticsearch.core.Tuple.tuple;

public class KqlParserExecutionContext {
public class KqlParsingContext {

private static final List<String> IGNORED_METADATA_FIELDS = List.of(
"_seq_no",
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ protected List<String> 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);
}

Expand Down

0 comments on commit 8503c6d

Please sign in to comment.