Skip to content

Commit c204137

Browse files
committed
Deprecate BoolQueryBuilder's mustNot field (#53125)
The bool query builder in elasticsearch accepts both must_not and mustNot fields. Given that leniency is abhorrent and must be eschewed, we should deprecate the latter as it doesn't fit with the style of parameters elsewhere in the DSL.
1 parent 2e924e4 commit c204137

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

server/src/main/java/org/elasticsearch/index/query/BoolQueryBuilder.java

+2-4
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,8 @@ public class BoolQueryBuilder extends AbstractQueryBuilder<BoolQueryBuilder> {
5353

5454
public static final boolean ADJUST_PURE_NEGATIVE_DEFAULT = true;
5555

56-
private static final ParseField MUSTNOT = new ParseField("mustNot"); // TODO deprecate?
57-
private static final ParseField MUST_NOT = new ParseField("must_not");
56+
private static final ParseField MUST_NOT = new ParseField("must_not")
57+
.withDeprecation("mustNot");
5858
private static final ParseField FILTER = new ParseField("filter");
5959
private static final ParseField SHOULD = new ParseField("should");
6060
private static final ParseField MUST = new ParseField("must");
@@ -284,8 +284,6 @@ private static void doXArrayContent(ParseField field, List<QueryBuilder> clauses
284284
SHOULD);
285285
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::mustNot), (p, c) -> parseInnerQueryBuilder(p),
286286
MUST_NOT);
287-
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::mustNot), (p, c) -> parseInnerQueryBuilder(p),
288-
MUSTNOT); // TODO should we deprecate this version?
289287
PARSER.declareObjectArray((builder, clauses) -> clauses.forEach(builder::filter), (p, c) -> parseInnerQueryBuilder(p),
290288
FILTER);
291289
PARSER.declareBoolean(BoolQueryBuilder::adjustPureNegative, ADJUST_PURE_NEGATIVE);

server/src/test/java/org/elasticsearch/index/query/BoolQueryBuilderTests.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ protected Map<String, BoolQueryBuilder> getAlternateVersions() {
137137
}
138138
if (tempQueryBuilder.mustNot().size() > 0) {
139139
QueryBuilder mustNot = tempQueryBuilder.mustNot().get(0);
140-
contentString += (randomBoolean() ? "\"must_not\": " : "\"mustNot\": ") + mustNot.toString() + ",";
140+
contentString += "\"must_not\":" + mustNot.toString() + ",";
141141
expectedQuery.mustNot(mustNot);
142142
}
143143
if (tempQueryBuilder.should().size() > 0) {
@@ -298,6 +298,14 @@ public void testUnknownQueryName() throws IOException {
298298

299299
}
300300

301+
public void testDeprecation() throws IOException {
302+
String query = "{\"bool\" : {\"mustNot\" : { \"match_all\" : { } } } }";
303+
QueryBuilder q = parseQuery(query);
304+
QueryBuilder expected = new BoolQueryBuilder().mustNot(new MatchAllQueryBuilder());
305+
assertEquals(expected, q);
306+
assertWarnings("Deprecated field [mustNot] used, expected [must_not] instead");
307+
}
308+
301309
public void testRewrite() throws IOException {
302310
BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
303311
boolean mustRewrite = false;

0 commit comments

Comments
 (0)