Skip to content

Commit

Permalink
Minor code quality improvement.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 8, 2024
1 parent 5ae733e commit a54233b
Showing 1 changed file with 13 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import static org.elasticsearch.xpack.kql.parser.KqlParsingContext.isRuntimeField;
import static org.elasticsearch.xpack.kql.parser.KqlParsingContext.isSearchableField;
import static org.elasticsearch.xpack.kql.parser.ParserUtils.escapeLuceneQueryString;
import static org.elasticsearch.xpack.kql.parser.ParserUtils.extractText;
import static org.elasticsearch.xpack.kql.parser.ParserUtils.hasWildcard;
import static org.elasticsearch.xpack.kql.parser.ParserUtils.typedParsing;

class KqlAstBuilder extends KqlBaseBaseVisitor<QueryBuilder> {
private final KqlParsingContext kqlParsingContext;
Expand All @@ -42,7 +44,7 @@ class KqlAstBuilder extends KqlBaseBaseVisitor<QueryBuilder> {
public QueryBuilder toQueryBuilder(ParserRuleContext ctx) {
if (ctx instanceof KqlBaseParser.TopLevelQueryContext topLeveQueryContext) {
if (topLeveQueryContext.query() != null) {
return ParserUtils.typedParsing(this, topLeveQueryContext.query(), QueryBuilder.class);
return typedParsing(this, topLeveQueryContext.query(), QueryBuilder.class);
}

return new MatchAllQueryBuilder();
Expand All @@ -63,9 +65,9 @@ public QueryBuilder visitAndBooleanQuery(KqlBaseParser.BooleanQueryContext ctx)
// TODO: KQLContext has an option to wrap the clauses into a filter instead of a must clause. Do we need it?
for (ParserRuleContext subQueryCtx : ctx.query()) {
if (subQueryCtx instanceof KqlBaseParser.BooleanQueryContext booleanSubQueryCtx && isAndQuery(booleanSubQueryCtx)) {
ParserUtils.typedParsing(this, subQueryCtx, BoolQueryBuilder.class).must().forEach(builder::must);
typedParsing(this, subQueryCtx, BoolQueryBuilder.class).must().forEach(builder::must);
} else {
builder.must(ParserUtils.typedParsing(this, subQueryCtx, QueryBuilder.class));
builder.must(typedParsing(this, subQueryCtx, QueryBuilder.class));
}
}

Expand All @@ -77,9 +79,9 @@ public QueryBuilder visitOrBooleanQuery(KqlBaseParser.BooleanQueryContext ctx) {

for (ParserRuleContext subQueryCtx : ctx.query()) {
if (subQueryCtx instanceof KqlBaseParser.BooleanQueryContext booleanSubQueryCtx && isOrQuery(booleanSubQueryCtx)) {
ParserUtils.typedParsing(this, subQueryCtx, BoolQueryBuilder.class).should().forEach(builder::should);
typedParsing(this, subQueryCtx, BoolQueryBuilder.class).should().forEach(builder::should);
} else {
builder.should(ParserUtils.typedParsing(this, subQueryCtx, QueryBuilder.class));
builder.should(typedParsing(this, subQueryCtx, QueryBuilder.class));
}
}

Expand All @@ -88,12 +90,12 @@ public QueryBuilder visitOrBooleanQuery(KqlBaseParser.BooleanQueryContext ctx) {

@Override
public QueryBuilder visitNotQuery(KqlBaseParser.NotQueryContext ctx) {
return QueryBuilders.boolQuery().mustNot(ParserUtils.typedParsing(this, ctx.simpleQuery(), QueryBuilder.class));
return QueryBuilders.boolQuery().mustNot(typedParsing(this, ctx.simpleQuery(), QueryBuilder.class));
}

@Override
public QueryBuilder visitParenthesizedQuery(KqlBaseParser.ParenthesizedQueryContext ctx) {
return ParserUtils.typedParsing(this, ctx.query(), QueryBuilder.class);
return typedParsing(this, ctx.query(), QueryBuilder.class);
}

@Override
Expand Down Expand Up @@ -125,7 +127,7 @@ public QueryBuilder visitExistsQuery(KqlBaseParser.ExistsQueryContext ctx) {
public QueryBuilder visitRangeQuery(KqlBaseParser.RangeQueryContext ctx) {
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1);

String queryText = ParserUtils.extractText(ctx.rangeQueryValue());
String queryText = extractText(ctx.rangeQueryValue());
BiFunction<RangeQueryBuilder, String, RangeQueryBuilder> rangeOperation = rangeOperation(ctx.operator);

withFields(ctx.fieldName(), (fieldName, mappedFieldType) -> {
Expand All @@ -143,7 +145,7 @@ public QueryBuilder visitRangeQuery(KqlBaseParser.RangeQueryContext ctx) {

@Override
public QueryBuilder visitFieldLessQuery(KqlBaseParser.FieldLessQueryContext ctx) {
String queryText = ParserUtils.extractText(ctx.fieldQueryValue());
String queryText = extractText(ctx.fieldQueryValue());

if (hasWildcard(ctx.fieldQueryValue())) {
QueryStringQueryBuilder queryString = QueryBuilders.queryStringQuery(escapeLuceneQueryString(queryText, true));
Expand Down Expand Up @@ -173,7 +175,7 @@ public QueryBuilder visitFieldLessQuery(KqlBaseParser.FieldLessQueryContext ctx)
public QueryBuilder visitFieldQuery(KqlBaseParser.FieldQueryContext ctx) {

BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().minimumShouldMatch(1);
String queryText = ParserUtils.extractText(ctx.fieldQueryValue());
String queryText = extractText(ctx.fieldQueryValue());
boolean hasWildcard = hasWildcard(ctx.fieldQueryValue());

withFields(ctx.fieldName(), (fieldName, mappedFieldType) -> {
Expand Down Expand Up @@ -215,7 +217,7 @@ private static boolean isOrQuery(KqlBaseParser.BooleanQueryContext ctx) {

private void withFields(KqlBaseParser.FieldNameContext ctx, BiConsumer<String, MappedFieldType> fieldConsummer) {
assert ctx != null : "Field ctx cannot be null";
String fieldNamePattern = ParserUtils.extractText(ctx);
String fieldNamePattern = extractText(ctx);
Set<String> fieldNames = kqlParsingContext.resolveFieldNames(fieldNamePattern);

if (ctx.value.getType() == KqlBaseParser.QUOTED_STRING && Regex.isSimpleMatchPattern(fieldNamePattern)) {
Expand Down

0 comments on commit a54233b

Please sign in to comment.