# Query string parentheses bug

**URL:** <https://discuss.elastic.co/t/query-string-parentheses-bug/63278>\
**Category:** Elasticsearch\
**Created:** [October 18, 2016, 10:14am UTC](https://discuss.elastic.co/t/query-string-parentheses-bug/63278 "2016-10-18T10:14:44Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 18, 2016, 10:14am UTC](https://discuss.elastic.co/t/query-string-parentheses-bug/63278/1 "2016-10-18T10:14:44Z")

</div>

Query string seems not respecting AND priority against OR.

In a very simple index with 3 documents:

> {  
> \_id: "2",  
> \_score: 1,  
> \_source: {  
> name: "vincent",  
> age: 20  
> }  
> },  
> {  
> \_id: "1",  
> \_score: 1,  
> \_source: {  
> name: "tom",  
> age: 25  
> }  
> },  
> {  
> \_id: "3",  
> \_score: 1,  
> \_source: {  
> name: "alicia",  
> age: 2  
> }  
> }

Why **(tom AND age:25) OR age:2** is not giving same results than **tom AND age:25 OR age:2**?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [October 19, 2016, 8:03am UTC](https://discuss.elastic.co/t/query-string-parentheses-bug/63278/2 "2016-10-19T08:03:15Z")

</div>

Hi @ebuildy,

Elasticsearch will turn your queries into Lucene queries internally. Here is what happens:

```auto
Elasticsearch: (tom AND age:25) OR age:2
Lucene: (+_all:tom +age: 25) age: 2 

```

Which means: "All documents that contain "tom" AND are 25 OR the ones that are 2". Both "tom" and "alica" match these criteria.

Whereas

```auto
Elasticsearch: tom AND age:25 OR age:2
Lucene: +_all:tom +age: 25 age: 2

```

means "all document that contain tom AND are 25. If they are 2 as well give them some more score".

As in this case, only "tom" matches.

I hope that helps.

Daniel

---

<div class="post-metadata">

**Author:** ![ebuildy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ebuildy/32/6070_2.png) [@ebuildy](https://discuss.elastic.co/u/ebuildy)\
**Post date:** [October 19, 2016, 8:23am UTC](https://discuss.elastic.co/t/query-string-parentheses-bug/63278/3 "2016-10-19T08:23:57Z")

</div>

True, is not "real" conditional, I forgot that!

Many thanks for the clarification.

I tried also "(name:tom AND age:18) OR age:2" VS "name:tom AND age:18 OR age:2" same thing.

BTW Is not possible to close the conversation in order to mark it as resolved?

---

<div class="post-metadata">

**Author:** ![danielmitterdorfer](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/danielmitterdorfer/32/110510_2.png) [@danielmitterdorfer](https://discuss.elastic.co/u/danielmitterdorfer)\
**Post date:** [October 19, 2016, 8:27am UTC](https://discuss.elastic.co/t/query-string-parentheses-bug/63278/4 "2016-10-19T08:27:54Z")

</div>

Hi @ebuildy,

Glad that I could help you.

> [@ebuildy](#):
>
> BTW Is not possible to close the conversation in order to mark it as resolved?

I think you need to click the little checkbox below my answer to indicate that this response solves your question. Then it should be marked as resolved.

Daniel

---

<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 5, 2017, 10:11pm UTC](https://discuss.elastic.co/t/query-string-parentheses-bug/63278/5 "2017-07-05T22:11:22Z")

</div>


