Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bool query behaviour on null value #56817

Merged
merged 3 commits into from
May 26, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -240,6 +240,24 @@ public <T> void declareObjectArray(BiConsumer<Value, List<T>> consumer, ContextP
declareFieldArray(consumer, (p, c) -> objectParser.parse(p, c), field, ValueType.OBJECT_ARRAY);
}

/**
* like {@link #declareObjectArray(BiConsumer, ContextParser, ParseField)}, but can also handle single null values,
* in which case the consumer isn't called
*/
public <
T> void declareObjectArrayOrNull(
BiConsumer<Value, List<T>> consumer,
ContextParser<Context, T> objectParser,
ParseField field
) {
declareField(
(value, list) -> { if (list != null) consumer.accept(value, list); },
(p, c) -> p.currentToken() == XContentParser.Token.VALUE_NULL ? null : parseArray(p, () -> objectParser.parse(p, c)),
field,
ValueType.OBJECT_ARRAY_OR_NULL
);
}

public void declareStringArray(BiConsumer<Value, List<String>> consumer, ParseField field) {
declareFieldArray(consumer, (p, c) -> p.text(), field, ValueType.STRING_ARRAY);
}
Original file line number Diff line number Diff line change
@@ -641,6 +641,7 @@ public enum ValueType {
OBJECT(START_OBJECT),
OBJECT_OR_NULL(START_OBJECT, VALUE_NULL),
OBJECT_ARRAY(START_OBJECT, START_ARRAY),
OBJECT_ARRAY_OR_NULL(START_OBJECT, START_ARRAY, VALUE_NULL),
OBJECT_OR_BOOLEAN(START_OBJECT, VALUE_BOOLEAN),
OBJECT_OR_STRING(START_OBJECT, VALUE_STRING),
OBJECT_OR_LONG(START_OBJECT, VALUE_NUMBER),
Original file line number Diff line number Diff line change
@@ -273,16 +273,16 @@ private static void doXArrayContent(ParseField field, List<QueryBuilder> clauses

private static final ObjectParser<BoolQueryBuilder, Void> PARSER = new ObjectParser<>("bool", BoolQueryBuilder::new);
static {
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::must), (p, c) -> parseInnerQueryBuilder(p),
PARSER.declareObjectArrayOrNull((builder, clauses) -> clauses.forEach(builder::must), (p, c) -> parseInnerQueryBuilder(p),
MUST);
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::should), (p, c) -> parseInnerQueryBuilder(p),
PARSER.declareObjectArrayOrNull((builder, clauses) -> clauses.forEach(builder::should), (p, c) -> parseInnerQueryBuilder(p),
SHOULD);
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::mustNot), (p, c) -> parseInnerQueryBuilder(p),
PARSER.declareObjectArrayOrNull((builder, clauses) -> clauses.forEach(builder::mustNot), (p, c) -> parseInnerQueryBuilder(p),
MUST_NOT);
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::filter), (p, c) -> parseInnerQueryBuilder(p),
PARSER.declareObjectArrayOrNull((builder, clauses) -> clauses.forEach(builder::filter), (p, c) -> parseInnerQueryBuilder(p),
FILTER);
PARSER.declareBoolean(BoolQueryBuilder::adjustPureNegative, ADJUST_PURE_NEGATIVE);
PARSER.declareField(BoolQueryBuilder::minimumShouldMatch, (p, c) -> p.text(),
PARSER.declareField(BoolQueryBuilder::minimumShouldMatch, (p, c) -> p.textOrNull(),
MINIMUM_SHOULD_MATCH, ObjectParser.ValueType.VALUE);
PARSER.declareString(BoolQueryBuilder::queryName, NAME_FIELD);
PARSER.declareFloat(BoolQueryBuilder::boost, BOOST_FIELD);
Original file line number Diff line number Diff line change
@@ -284,6 +284,36 @@ public void testMinimumShouldMatchNumber() throws IOException {
assertEquals("1", builder.minimumShouldMatch());
}

public void testMinimumShouldMatchNull() throws IOException {
String query = "{\"bool\" : {\"must\" : { \"term\" : { \"field\" : \"value\" } }, \"minimum_should_match\" : null } }";
BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query);
assertEquals(null, builder.minimumShouldMatch());
}

public void testMustNull() throws IOException {
String query = "{\"bool\" : {\"must\" : null } }";
BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query);
assertTrue(builder.must().isEmpty());
}

public void testMustNotNull() throws IOException {
String query = "{\"bool\" : {\"must_not\" : null } }";
BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query);
assertTrue(builder.mustNot().isEmpty());
}

public void testShouldNull() throws IOException {
String query = "{\"bool\" : {\"should\" : null } }";
BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query);
assertTrue(builder.should().isEmpty());
}

public void testFilterNull() throws IOException {
String query = "{\"bool\" : {\"filter\" : null } }";
BoolQueryBuilder builder = (BoolQueryBuilder) parseQuery(query);
assertTrue(builder.filter().isEmpty());
}

/**
* test that unknown query names in the clauses throw an error
*/