# Operands priorities in Elasticsearch query

**URL:** <https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759>\
**Category:** Elasticsearch\
**Created:** [September 4, 2016, 11:44am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759 "2016-09-04T11:44:33Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![nivke2000](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@nivke2000](https://discuss.elastic.co/u/nivke2000)\
**Post date:** [September 4, 2016, 11:44am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/1 "2016-09-04T11:44:33Z")

</div>

Hi,  
I am researching the Elasticsearch engine. I would like to know which operand takes precedence on which operands. The only reference I was able to find is:  
**NOT takes precedence over AND, which takes precedence over OR.**  
_([https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html))_

I need to know where is the Proximity operand falls in?  
Anyone can help? If there's any place this is documented I will be happy to get the URL.

Thanks,  
Niv

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [September 5, 2016, 9:27am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/2 "2016-09-05T09:27:00Z")

</div>

The proximity search is not a boolean operator so the priority does not apply to this part of the query. Can you provide us with an example of query where the priority is not clear for you ?

---

<div class="post-metadata">

**Author:** ![nivke2000](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@nivke2000](https://discuss.elastic.co/u/nivke2000)\
**Post date:** [September 5, 2016, 10:25am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/3 "2016-09-05T10:25:52Z")

</div>

Hi,  
Thanks for your answer.  
I don't have a specific example. I was making a comparison between KQL that engines like FAST use, and Elasticsearch.  
For KQL I found this: _[https://msdn.microsoft.com/en-us/library/hh658228(v=office.12).aspx](https://msdn.microsoft.com/en-us/library/hh658228(v=office.12).aspx)_  
As you can see, it prioritize all operators: boolean and non-boolean.

So, I though Elasticsearch will have something similar.

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [September 5, 2016, 10:43am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/4 "2016-09-05T10:43:43Z")

</div>

Yes this is because they have an operator named NEAR. For the query\_string we don't have a NEAR operator but you can use the slop parameter of the phrase query:

```auto
"fox quick"~5 AND brown

```

In such case the proximity operator is not ambiguous, it should apply to the phrase query only as a filter. With an operator like NEAR, the ambiguity is resolved by the priority of the operators like in:

```auto
fox NEAR quick AND brown

```

Since the `query_string` query does not implement such operator we don't need priority.

---

<div class="post-metadata">

**Author:** ![nivke2000](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@nivke2000](https://discuss.elastic.co/u/nivke2000)\
**Post date:** [September 5, 2016, 10:47am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/5 "2016-09-05T10:47:20Z")

</div>

Thank you very much for this clear answer.  
It really helps.

---

<div class="post-metadata">

**Author:** ![nivke2000](https://avatars.discourse-cdn.com/v4/letter/n/a88e4f/32.png) [@nivke2000](https://discuss.elastic.co/u/nivke2000)\
**Post date:** [September 6, 2016, 8:45am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/6 "2016-09-06T08:45:35Z")

</div>

Hi,  
One more question:  
can I write one of the following queries: **“fox quick or brown”~4** , or **“fox (quick or brown)”~4**  
so the "or" is calculated before the "proximity"?

---

<div class="post-metadata">

**Author:** ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)\
**Post date:** [September 6, 2016, 8:58am UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/7 "2016-09-06T08:58:54Z")

</div>

No you can't. This is a phrase query and the "or" inside the quote is treated as a normal word.  
Though you can achieve what you asked with a span query:  
[https://www.elastic.co/guide/en/elasticsearch/reference/current/span-queries.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/span-queries.html)

```auto
 "span_near": {
      "clauses": [
         {
            "span_term": {
               "field": "fox"
            }
         },
         {
            "span_or": {
               "clauses": [
                  {
                     "span_term": {
                        "field": "quick"
                     }
                  },
                  {
                     "span_term": {
                        "field": "brown"
                     }
                  }
               ]
            }
         }
      ],
      "slop": 5,
      "in_order": true,
      "collect_payloads": false
   }

```

---

<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:22pm UTC](https://discuss.elastic.co/t/operands-priorities-in-elasticsearch-query/59759/8 "2017-07-05T22:22:21Z")

</div>


