Skip to content

Commit 527063a

Browse files
authored
Add BooleanQuery.hasClauses() (#525) (#527)
1 parent 0b9456d commit 527063a

File tree

2 files changed

+67
-0
lines changed

2 files changed

+67
-0
lines changed

java-client/src/main/java/co/elastic/clients/elasticsearch/_types/query_dsl/BoolQuery.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -331,6 +331,16 @@ public BoolQuery build() {
331331

332332
return new BoolQuery(this);
333333
}
334+
335+
/**
336+
* Returns <code>true</code> if this query has at least one should, must, must
337+
* not or filter clause.
338+
*/
339+
public boolean hasClauses() {
340+
return !(this.must == null || this.must.isEmpty()) || !(this.mustNot == null || this.mustNot.isEmpty())
341+
|| !(this.should == null || this.should.isEmpty())
342+
|| !(this.filter == null || this.filter.isEmpty());
343+
}
334344
}
335345

336346
// ---------------------------------------------------------------------------------------------
@@ -351,4 +361,12 @@ protected static void setupBoolQueryDeserializer(ObjectDeserializer<BoolQuery.Bu
351361

352362
}
353363

364+
/**
365+
* Returns <code>true</code> if this query has at least one should, must, must
366+
* not or filter clause.
367+
*/
368+
public boolean hasClauses() {
369+
return !(this.must == null || this.must.isEmpty()) || !(this.mustNot == null || this.mustNot.isEmpty())
370+
|| !(this.should == null || this.should.isEmpty()) || !(this.filter == null || this.filter.isEmpty());
371+
}
354372
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Licensed to Elasticsearch B.V. under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch B.V. licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package co.elastic.clients.elasticsearch.model;
21+
22+
import co.elastic.clients.elasticsearch._types.query_dsl.BoolQuery;
23+
import co.elastic.clients.elasticsearch._types.query_dsl.QueryBuilders;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.Test;
26+
27+
public class CodeAdditionsTests extends Assertions {
28+
29+
@Test
30+
public void testHasClauses() {
31+
32+
{
33+
BoolQuery.Builder builder = new BoolQuery.Builder();
34+
assertFalse(builder.hasClauses());
35+
36+
BoolQuery query = builder.build();
37+
assertFalse(query.hasClauses());
38+
}
39+
40+
{
41+
BoolQuery.Builder builder = new BoolQuery.Builder();
42+
builder.must(QueryBuilders.matchAll(b -> b));
43+
assertTrue(builder.hasClauses());
44+
45+
BoolQuery query = builder.build();
46+
assertTrue(query.hasClauses());
47+
}
48+
}
49+
}

0 commit comments

Comments
 (0)