# How lucene query works

**URL:** <https://discuss.elastic.co/t/how-lucene-query-works/126232>\
**Category:** Elasticsearch\
**Created:** [March 30, 2018, 1:23pm UTC](https://discuss.elastic.co/t/how-lucene-query-works/126232 "2018-03-30T13:23:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![meetdave2611997](https://avatars.discourse-cdn.com/v4/letter/m/f07891/32.png) [@meetdave2611997](https://discuss.elastic.co/u/meetdave2611997)\
**Post date:** [March 30, 2018, 1:23pm UTC](https://discuss.elastic.co/t/how-lucene-query-works/126232/1 "2018-03-30T13:23:25Z")

</div>

I am having problem with this query and i want to understand why this is happening and how lucene evaluates fields values.

`(id:1 OR id:2) OR (type:1 OR type:2) AND product:*` this query gives me 1000 results

`product:* AND (id:1 OR id:2) OR (type:1 OR type:2)` this query gives me 994 results

And the correct answer is 994.

Can anyone tell me how this works? and why this is happening?

I am also having same issue in 2 other queries. If i interchange position of product results also change.

Thanks,  
Meet Dave

---

<div class="post-metadata">

**Author:** ![atira](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/atira/32/28699_2.png) [@atira](https://discuss.elastic.co/u/atira)\
**Post date:** [March 30, 2018, 1:45pm UTC](https://discuss.elastic.co/t/how-lucene-query-works/126232/2 "2018-03-30T13:45:27Z")

</div>

Isn't logical operators evaluated in order?  
Let's assume that `(id:1 OR id:2)` is evaluated to true and `(type:1 OR type:2)` to true, `product:*` is false.

`(id:1 OR id:2) OR (type:1 OR type:2)`  
true OR true == true  
`AND product:*`  
true AND false == **false**

`product:* AND (id:1 OR id:2)`  
false AND true == false  
`OR (type:1 OR type:2)`  
false OR true == **true**

If `product:*` evaluates to false, then its position does matter because of the different operators.

edit: to summarize it.

If `product:*` is false, the first query will always evaluate to **false** , regardless of the other fields.

If `type` is `1` or `2`, the second query will always evaluate to **true** , regardless of any other field. `product:*` evaluation won't matter.

---

<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:** [April 27, 2018, 1:45pm UTC](https://discuss.elastic.co/t/how-lucene-query-works/126232/3 "2018-04-27T13:45:33Z")

</div>

Closed on April 27, 2018, 1:45pm UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
