Skip to content

Commit

Permalink
Range query tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Oct 30, 2024
1 parent 5f9be1a commit 5f4a003
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -262,6 +266,26 @@ public void testParseWildcardKeywordFieldQuery() {
assertWildcardQueryBuilder(parseKqlQuery(kqlFieldQuery(KEYWORD_FIELD_NAME, "NOT fo*")), KEYWORD_FIELD_NAME, "NOT fo*");
}

public void testFieldWildCardQueries() {
List<String> queries = List.of("foo", "foo bar", quoteString("foo"), "foo*");
List<String> 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());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> queryValueSamples() {
return Stream.of(randomIdentifier(), randomTimeValue(), randomInt(), randomDouble()).map(Object::toString).toList();
}

private static String quoteString(String input) {
return Strings.format("\"%s\"", input);
}
}

0 comments on commit 5f4a003

Please sign in to comment.