Mihir
Mihir

Reputation: 31

Why elastic search query with parenthesis and without parenthesis give different output?

I have 2 below elastic search query

This give 5 result

POST twitter/object/_search
{

 "_source": false,
 "query": {
   "query_string": {
     "query": "Apple AND Orange OR Banana",
     "default_field": "content"
    }
  }
}

This give 12 result

POST twitter/object/_search
{

 "_source": false,
 "query": {
   "query_string": {
     "query": "(Apple AND Orange) OR Banana",
     "default_field": "content"
    }
  }
}

Please help me out why it's happening. How the query is interpret because logically both queries should give same output?

Upvotes: 2

Views: 2137

Answers (1)

Adam T
Adam T

Reputation: 1691

From https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_boolean_operators

The familiar boolean operators AND, OR and NOT are supported but beware that they do not honor the usual precedence rules, so parentheses should be used whenever multiple operators are used together.

Upvotes: 1

Related Questions