From 5f4a0038339b4dec3fc6efe00b721aa3ad2d4d01 Mon Sep 17 00:00:00 2001 From: Aurelien FOUCRET Date: Wed, 30 Oct 2024 18:09:28 +0100 Subject: [PATCH] Range query tests. --- .../kql/parser/KqlParserFieldQueryTests.java | 24 ++++++ .../kql/parser/KqlParserRangeQueryTests.java | 84 +++++++++++++++++++ 2 files changed, 108 insertions(+) create mode 100644 x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserRangeQueryTests.java diff --git a/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserFieldQueryTests.java b/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserFieldQueryTests.java index 5c4bbd7fec81e..80ece5c1da18b 100644 --- a/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserFieldQueryTests.java +++ b/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserFieldQueryTests.java @@ -8,11 +8,15 @@ package org.elasticsearch.xpack.kql.parser; import org.elasticsearch.core.Strings; +import org.elasticsearch.index.query.BoolQueryBuilder; import org.elasticsearch.index.query.MatchNoneQueryBuilder; import java.util.List; +import static org.hamcrest.Matchers.containsInAnyOrder; +import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; import static org.hamcrest.Matchers.isA; public class KqlParserFieldQueryTests extends AbstractKqlParserTestCase { @@ -262,6 +266,26 @@ public void testParseWildcardKeywordFieldQuery() { assertWildcardQueryBuilder(parseKqlQuery(kqlFieldQuery(KEYWORD_FIELD_NAME, "NOT fo*")), KEYWORD_FIELD_NAME, "NOT fo*"); } + public void testFieldWildCardQueries() { + List queries = List.of("foo", "foo bar", quoteString("foo"), "foo*"); + List mappedFieldName = mappedLeafFields(); + for (String query : queries) { + String kqlQuery = kqlFieldQuery("mapped_*", query); + BoolQueryBuilder parsedQuery = asInstanceOf(BoolQueryBuilder.class, parseKqlQuery(kqlQuery)); + + assertThat(parsedQuery.mustNot(), empty()); + assertThat(parsedQuery.must(), empty()); + assertThat(parsedQuery.filter(), empty()); + assertThat(parsedQuery.minimumShouldMatch(), equalTo("1")); + assertThat(parsedQuery.should(), hasSize(mappedFieldName.size())); + + assertThat( + parsedQuery.should(), + containsInAnyOrder(mappedFieldName.stream().map(fieldName -> parseKqlQuery(kqlFieldQuery(fieldName, query))).toArray()) + ); + } + } + private static String kqlFieldQuery(String field, Object value) { return wrapWithRandomWhitespaces(field) + wrapWithRandomWhitespaces(":") + wrapWithRandomWhitespaces(value.toString()); } diff --git a/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserRangeQueryTests.java b/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserRangeQueryTests.java new file mode 100644 index 0000000000000..8a076d611b5f8 --- /dev/null +++ b/x-pack/plugin/kql/src/test/java/org/elasticsearch/xpack/kql/parser/KqlParserRangeQueryTests.java @@ -0,0 +1,84 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +package org.elasticsearch.xpack.kql.parser; + +import org.elasticsearch.core.Strings; + +import java.util.List; +import java.util.stream.Stream; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.nullValue; + +public class KqlParserRangeQueryTests extends AbstractKqlParserTestCase { + + public void testParseLtQuery() { + for (String fieldName : List.of(DATE_FIELD_NAME, TEXT_FIELD_NAME, INT_FIELD_NAME, DOUBLE_FIELD_NAME, KEYWORD_FIELD_NAME)) { + for (String query : queryValueSamples()) { + assertRangeQueryBuilder(parseKqlQuery(Strings.format("%s < %s", fieldName, query)), fieldName, (rangeQuery) -> { + assertThat(rangeQuery.from(), nullValue()); + assertThat(rangeQuery.to(), equalTo(query)); + assertThat(rangeQuery.includeUpper(), equalTo(false)); + + // For range query adding quote does not change the generated of the query. + assertThat(parseKqlQuery(Strings.format("%s < %s", fieldName, quoteString(query))), equalTo(rangeQuery)); + }); + } + } + } + + public void testParseLteQuery() { + for (String fieldName : List.of(DATE_FIELD_NAME, TEXT_FIELD_NAME, INT_FIELD_NAME, DOUBLE_FIELD_NAME, KEYWORD_FIELD_NAME)) { + for (String query : queryValueSamples()) { + assertRangeQueryBuilder(parseKqlQuery(Strings.format("%s <= %s", fieldName, query)), fieldName, (rangeQuery) -> { + assertThat(rangeQuery.from(), nullValue()); + assertThat(rangeQuery.to(), equalTo(query)); + assertThat(rangeQuery.includeUpper(), equalTo(true)); + // For range query adding quote does not change the generated of the query. + assertThat(parseKqlQuery(Strings.format("%s <= %s", fieldName, quoteString(query))), equalTo(rangeQuery)); + }); + } + } + } + + public void testParseGtQuery() { + for (String fieldName : List.of(DATE_FIELD_NAME, TEXT_FIELD_NAME, INT_FIELD_NAME, DOUBLE_FIELD_NAME, KEYWORD_FIELD_NAME)) { + for (String query : queryValueSamples()) { + assertRangeQueryBuilder(parseKqlQuery(Strings.format("%s > %s", fieldName, query)), fieldName, (rangeQuery) -> { + assertThat(rangeQuery.to(), nullValue()); + assertThat(rangeQuery.from(), equalTo(query)); + assertThat(rangeQuery.includeLower(), equalTo(false)); + // For range query adding quote does not change the generated of the query. + assertThat(parseKqlQuery(Strings.format("%s > %s", fieldName, quoteString(query))), equalTo(rangeQuery)); + }); + } + } + } + + public void testParseGteQuery() { + for (String fieldName : List.of(DATE_FIELD_NAME, TEXT_FIELD_NAME, INT_FIELD_NAME, DOUBLE_FIELD_NAME, KEYWORD_FIELD_NAME)) { + for (String query : queryValueSamples()) { + assertRangeQueryBuilder(parseKqlQuery(Strings.format("%s >= %s", fieldName, query)), fieldName, (rangeQuery) -> { + assertThat(rangeQuery.to(), nullValue()); + assertThat(rangeQuery.from(), equalTo(query)); + assertThat(rangeQuery.includeLower(), equalTo(true)); + // For range query adding quote does not change the generated of the query. + assertThat(parseKqlQuery(Strings.format("%s >= %s", fieldName, quoteString(query))), equalTo(rangeQuery)); + }); + } + } + } + + private static List queryValueSamples() { + return Stream.of(randomIdentifier(), randomTimeValue(), randomInt(), randomDouble()).map(Object::toString).toList(); + } + + private static String quoteString(String input) { + return Strings.format("\"%s\"", input); + } +}