# Bool Query Should clause in elasticsearch vs java OR condition?

**URL:** <https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690>\
**Category:** Elasticsearch\
**Created:** [May 21, 2018, 6:02pm UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690 "2018-05-21T18:02:27Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)\
**Post date:** [May 21, 2018, 6:02pm UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/1 "2018-05-21T18:02:28Z")

</div>

```
{

```

"bool": {  
"should": [  
{ "term": { "title": "brown" }} , -----\> 1.  
{ "term": { "title": "fox" }}  
]  
}  
}

I am new into the Elasticsearch world need help in understanding the " **bool**" query " **should**" clause, as mentioned in few blogs and documentation, " **Should**" Clause in elasticsearch is like OR condition, where at least one or more conditions should be true to get the response depending on the "minimum\_should\_match" parameter.  
The way i am visualizing OR condition in **elasticsearch** query is like Java OR condition where it will exit the flow with first encounter of **True** condition (1 in above code example), skipping remaining part of the flow. But with examples provided in Documentation It is clear that it is executing all the conditions, How can i avoid it??

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 21, 2018, 6:10pm UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/2 "2018-05-21T18:10:58Z")

</div>

Are you seeing a performance problem with the query? Where in the documentation is it indicated that this is not optimized?

---

<div class="post-metadata">

**Author:** ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)\
**Post date:** [May 22, 2018, 4:45am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/3 "2018-05-22T04:45:38Z")

</div>

Thanks @Christian_Dahlqvist for the reply, yes I am more concerned about the performance of the query as i have millions of record, which is growing large day by day.

**Requirement is when i fire the below query**

```
 {
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "4": {
              "query": "kavana",
              "_name": "4.WordMatch"
            }
          }
        },
        {
          "match": {
            "4.exactMatch": {
              "query": "kavana",
              "_name": "4.exactMatch"
            }
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

```

I am getting below result which is executing true for both **match query**

**Output**

`"matched_queries": ["4.WordMatch", "4.exactMatch"]`

Requirement is If Match is happend for first **match clause** then I don't want check remaining match cases of Query because of it will decrease search performance and it will increase documents count. Expectation is **Should** work like java **IF** condition.

**Expected output is**

```
"matched_queries": ["4.WordMatch"]

or
"matched_queries": ["4.exactMatch"]

```

Or Is there anyway i can optimise this, so i get best performance out of it.??

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 22, 2018, 6:24am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/4 "2018-05-22T06:24:43Z")

</div>

I am not sure I understand what the problem is. Can you please provide a minimal recreation example with data and query and show what result you are getting and what you are expecting?

---

<div class="post-metadata">

**Author:** ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)\
**Post date:** [May 22, 2018, 7:36am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/5 "2018-05-22T07:36:25Z")

</div>

@Christian_Dahlqvist

I have a simple JSON query for Elasticsearch that looks like this:

```
   {
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "id": "1"
          }
        },
        {
          "match": {
            "tags.name": "a1"
          }
        }
      ]
    }
  }
}

```

Actual output is,

**`1, a1`**

Means it will evaluate the **both cases** and based on scoring it will return the output.

**Expected output**

Either **1** OR **a1**

It should not execute second match case when first match case is executed to true or return some matching result.

In simple terms it should not evaluate second match condition itself when first condition is validated to true or returned some value.

How to achieve it?

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 22, 2018, 7:41am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/6 "2018-05-22T07:41:56Z")

</div>

What does the documents look like?

---

<div class="post-metadata">

**Author:** ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)\
**Post date:** [May 22, 2018, 8:54am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/7 "2018-05-22T08:54:39Z")

</div>

```
{
   "_index": "example",
      "_type": "blogpost",
      "_id": "4",
      "_score": 1.0,
      "_source": {
        "id": "1",
        "title": "I hate cheese cake",
        "body": "I prefer chocolat cake",
        "tags":{
        "name" : ["a1", "c1"]
         } ,
        "status": "archived",
        "publish_date": "1985-08-03"
      }
    }, {
      "_index": "example",
      "_type": "blogpost",
      "_id": "5",
      "_score": 1.0,
      "_source": {
        "id": "2",
        "title": "I admire lions",
        "body": "They are so regal",
        "tags": {
         "name" : ["a1", "b1"]
         }
        "status": "published",
        "publish_date": "2016-08-02"
      }
    }
```

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 22, 2018, 8:59am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/8 "2018-05-22T08:59:21Z")

</div>

`minimum_should_match` indicates that at least one of the two `should` clauses should match, not that it should only return 1 document, so I believe what you are seeing is correct. If you are looking to only return a single match as quickly as possible, you might be able to run a filter query with size set to 1.

---

<div class="post-metadata">

**Author:** ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)\
**Post date:** [May 22, 2018, 9:00am UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/9 "2018-05-22T09:00:43Z")

</div>

Thanks @Christian_Dahlqvist could you please provide link to any such example to refer? it would be helpful.

---

<div class="post-metadata">

**Author:** ![san88922](https://avatars.discourse-cdn.com/v4/letter/s/ea666f/32.png) [@san88922](https://discuss.elastic.co/u/san88922)\
**Post date:** [May 23, 2018, 5:03pm UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/10 "2018-05-23T17:03:44Z")

</div>

@Christian_Dahlqvist Any update on this? or you need more information? please let me know..

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [May 23, 2018, 5:26pm UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/11 "2018-05-23T17:26:50Z")

</div>

I think it should be enough to [create a filter](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-filter-context.html) (so score does not have to be calculated for all hits), and then set `size` for the query to the number of results you want back.

---

<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:** [June 20, 2018, 5:26pm UTC](https://discuss.elastic.co/t/bool-query-should-clause-in-elasticsearch-vs-java-or-condition/132690/12 "2018-06-20T17:26:52Z")

</div>

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