Skip to content

Commit

Permalink
Fix existing tests to handle nested fields.
Browse files Browse the repository at this point in the history
  • Loading branch information
afoucret committed Nov 8, 2024
1 parent 066b60e commit 86d4d2d
Showing 1 changed file with 15 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.ExistsQueryBuilder;
import org.elasticsearch.index.query.MatchNoneQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.NestedQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;

import java.util.regex.Pattern;

import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.empty;
Expand All @@ -35,11 +38,18 @@ public void testParseExistsQueryWithNoMatchingFields() {

public void testParseExistsQueryWithASingleField() {
for (String fieldName : searchableFields()) {
ExistsQueryBuilder parsedQuery = asInstanceOf(ExistsQueryBuilder.class, parseKqlQuery(kqlExistsQuery(fieldName)));
assertThat(parsedQuery.fieldName(), equalTo(fieldName));
QueryBuilder parsedQuery = parseKqlQuery(kqlExistsQuery(fieldName));

// Using quotes to wrap the field name does not change the result.
assertThat(parseKqlQuery(kqlExistsQuery("\"" + fieldName + "\"")), equalTo(parsedQuery));

long nestingLevel = Pattern.compile("[.]").splitAsStream(fieldName).takeWhile(s -> s.equals(NESTED_FIELD_NAME)).count();
for (int i = 0; i < nestingLevel; i++) {
parsedQuery = asInstanceOf(NestedQueryBuilder.class, parsedQuery).query();
}

ExistsQueryBuilder existsQuery = asInstanceOf(ExistsQueryBuilder.class, parsedQuery);
assertThat(existsQuery.fieldName(), equalTo(fieldName));
}
}

Expand All @@ -53,7 +63,8 @@ public void testParseExistsQueryUsingWildcardFieldName() {

assertThat(
parsedQuery.should(),
containsInAnyOrder(searchableFields(fieldNamePattern).stream().map(QueryBuilders::existsQuery).toArray())
containsInAnyOrder(
searchableFields(fieldNamePattern).stream().map(fieldName -> parseKqlQuery(kqlExistsQuery(fieldName))).toArray())
);
}

Expand Down

0 comments on commit 86d4d2d

Please sign in to comment.