# Possible bug in BoolQueryBuilder - can't get multiple 'should' queries to work

**URL:** https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416
**Category:** Elasticsearch
**Created:** [May 16, 2011, 5:17pm UTC](https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416 "2011-05-16T17:17:01Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)
#### Post date: [May 16, 2011, 5:17pm UTC](https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416/1 "2011-05-16T17:17:01Z")

</div>

I'm trying to build a bool query that has multiple 'should' queries  
using the Java API (or the groovy API, for that matter), but it isn't  
working. I threw together a spock specification to test the behavior  
and confirmed it seems to be constructing the should clauses  
incorrectly. The spec is here:

> <https://gist.github.com/ellerycrane/974865>

It appears that the builder is adding multiple 'should' properties,  
and then aggregating their contents incorrectly. In the spec above,  
the query generates the following JSON:

{  
"bool": {  
"should": {  
"query\_string": {  
"query": "charles",  
"fields": ["user.name.first", "user.email",  
"user.name.last", "user.email"],  
"use\_dis\_max": true  
}  
},  
"should": {  
"query\_string": {  
"query": "xavier",  
"use\_dis\_max": true  
}  
}  
}  
}

However, it should instead be generating something like the following:

{  
"bool": {  
"should": {  
{  
"query\_string": {  
"query": "charles",  
"fields": ["user.name.first", "user.email"],  
"use\_dis\_max": true  
}  
},  
{  
"query\_string": {  
"query": "xavier",  
"fields": ["user.name.last", "user.email"],  
"use\_dis\_max": true  
}  
}  
}  
}  
}

As an additional note, I only tried the Java api after I couldn't get  
the Groovy dsl to work at all for should clauses. It might be related  
to the same bug, but perhaps not. Could anyone post an example of how  
to construct a bool query with multiple 'should' queries using the  
Groovy query DSL?

---

<div class="post-metadata">

### Author: ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)
#### Post date: [May 16, 2011, 7:46pm UTC](https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416/2 "2011-05-16T19:46:44Z")

</div>

The generated json (though not formally json, since it has several elements with the same name, like "should") is fine when it comes to elasticsearch (it supports that in its parsing stage).  
On Monday, May 16, 2011 at 8:17 PM, Ellery Crane wrote:

> I'm trying to build a bool query that has multiple 'should' queries  
> using the Java API (or the groovy API, for that matter), but it isn't  
> working. I threw together a spock specification to test the behavior  
> and confirmed it seems to be constructing the should clauses  
> incorrectly. The spec is here:
> 
> [elasticsearch java api should clause spec · GitHub](https://gist.github.com/974865)
> 
> It appears that the builder is adding multiple 'should' properties,  
> and then aggregating their contents incorrectly. In the spec above,  
> the query generates the following JSON:
> 
> {  
> "bool": {  
> "should": {  
> "query\_string": {  
> "query": "charles",  
> "fields": ["user.name.first", "user.email",  
> "user.name.last", "user.email"],  
> "use\_dis\_max": true  
> }  
> },  
> "should": {  
> "query\_string": {  
> "query": "xavier",  
> "use\_dis\_max": true  
> }  
> }  
> }  
> }
> 
> However, it should instead be generating something like the following:
> 
> {  
> "bool": {  
> "should": {  
> {  
> "query\_string": {  
> "query": "charles",  
> "fields": ["user.name.first", "user.email"],  
> "use\_dis\_max": true  
> }  
> },  
> {  
> "query\_string": {  
> "query": "xavier",  
> "fields": ["user.name.last", "user.email"],  
> "use\_dis\_max": true  
> }  
> }  
> }  
> }  
> }
> 
> As an additional note, I only tried the Java api after I couldn't get  
> the Groovy dsl to work at all for should clauses. It might be related  
> to the same bug, but perhaps not. Could anyone post an example of how  
> to construct a bool query with multiple 'should' queries using the  
> Groovy query DSL?

---

<div class="post-metadata">

### Author: ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)
#### Post date: [May 16, 2011, 8:05pm UTC](https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416/3 "2011-05-16T20:05:43Z")

</div>

I get an error when I try to run it. Also, the query json that it  
generates is wrong: it puts all the fields into the first should  
query, instead of putting each field in the query it was a part of.

On May 16, 3:46 pm, Shay Banon [shay.ba...@elasticsearch.com](mailto:shay.ba...@elasticsearch.com) wrote:

> The generated json (though not formally json, since it has several elements with the same name, like "should") is fine when it comes to elasticsearch (it supports that in its parsing stage).
> 
> On Monday, May 16, 2011 at 8:17 PM, Ellery Crane wrote:
> 
> > I'm trying to build a bool query that has multiple 'should' queries  
> > using the Java API (or the groovy API, for that matter), but it isn't  
> > working. I threw together a spock specification to test the behavior  
> > and confirmed it seems to be constructing the should clauses  
> > incorrectly. The spec is here:
> 
> > [elasticsearch java api should clause spec · GitHub](https://gist.github.com/974865)
> 
> > It appears that the builder is adding multiple 'should' properties,  
> > and then aggregating their contents incorrectly. In the spec above,  
> > the query generates the following JSON:
> 
> > {  
> > "bool": {  
> > "should": {  
> > "query\_string": {  
> > "query": "charles",  
> > "fields": ["user.name.first", "user.email",  
> > "user.name.last", "user.email"],  
> > "use\_dis\_max": true  
> > }  
> > },  
> > "should": {  
> > "query\_string": {  
> > "query": "xavier",  
> > "use\_dis\_max": true  
> > }  
> > }  
> > }  
> > }
> 
> > However, it should instead be generating something like the following:
> 
> > {  
> > "bool": {  
> > "should": {  
> > {  
> > "query\_string": {  
> > "query": "charles",  
> > "fields": ["user.name.first", "user.email"],  
> > "use\_dis\_max": true  
> > }  
> > },  
> > {  
> > "query\_string": {  
> > "query": "xavier",  
> > "fields": ["user.name.last", "user.email"],  
> > "use\_dis\_max": true  
> > }  
> > }  
> > }  
> > }  
> > }
> 
> > As an additional note, I only tried the Java api after I couldn't get  
> > the Groovy dsl to work at all for should clauses. It might be related  
> > to the same bug, but perhaps not. Could anyone post an example of how  
> > to construct a bool query with multiple 'should' queries using the  
> > Groovy query DSL?

---

<div class="post-metadata">

### Author: ![Ellery\_Crane\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ellery_crane_2/32/3369_2.png) [@Ellery\_Crane\_2](https://discuss.elastic.co/u/Ellery_Crane_2)
#### Post date: [May 16, 2011, 8:52pm UTC](https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416/4 "2011-05-16T20:52:48Z")

</div>

On May 16, 4:05 pm, Ellery Crane [seid...@gmail.com](mailto:seid...@gmail.com) wrote:

> I get an error when I try to run it. Also, the query json that it  
> generates is wrong: it puts all the fields into the first should  
> query, instead of putting each field in the query it was a part of.

Disregard that, evidently- chalk this up to user error 🙂 The fields  
issue was because there was a bug in my test code. I was, however,  
seeing exceptions when building a similar query yesterday- I can no  
longer produce them today, however; the code hasn't changed, but I am  
no longer seeing the errors. Perhaps something in my environment went  
awry; if I see them again, I will post a stack trace.

However, if anyone can point me to an example of how to do a should  
query using the Groovy DSL, I'd still be interested.

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [July 6, 2017, 4:06am UTC](https://discuss.elastic.co/t/possible-bug-in-boolquerybuilder-cant-get-multiple-should-queries-to-work/4416/5 "2017-07-06T04:06:04Z")

</div>


