# Should combining bool queries does not works

**URL:** <https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772>\
**Category:** Elasticsearch\
**Created:** [April 19, 2016, 10:13am UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772 "2016-04-19T10:13:47Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Juan\_Diaz\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Juan\_Diaz\_Gonzalez](https://discuss.elastic.co/u/Juan_Diaz_Gonzalez)\
**Post date:** [April 19, 2016, 10:13am UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/1 "2016-04-19T10:13:47Z")

</div>

I have this query in Elasticsearch 1.4.4:

```
`GET my_jobs/type_job/_search

{ "fields": [
   "id","user","state","start_time","end_time"
], 
    "query": {
        "bool": {
            "must": [
                
                {"term": {"user": {"value": "oid:user:id"}}},
                {"term": {"id": {"value": "oid:job:301:7544:1"}}}
               
                
                ], 
            "must_not": [
                {"term": {"state": {"value": "queued"}}}, 
                {"term": {"queue": {"value": "desktop.q"}}}], 
            "should": [
                {"bool": {
                    "must": [
                        {"term": {"state": {"value": "finished"}}}, 
                        {"range": {"start_time": {"lt": 1460930400}}}, 
                        {"range": {"end_time": {"gt": 1459461600}}}]
                    }}, 
                {"bool": {
                    "must": [
                        {"term": {"state": {"value": "started"}}}, 
                        {"range": {"start_time": {"lt": 1460930400}}}]}}
            ]
        }
    }
}

```

But I am obtaining this:

```
{
   "took": 3,
   "timed_out": false,
   "_shards": {
      "total": 5,
      "successful": 5,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 8.948642,
      "hits": [
         {
            "_index": "my_jobs",
            "_type": "type_job",
            "_id": "oid:job:301:7544:1",
            "_score": 8.948642,
            "fields": {
               "id": [
                  "oid:job:301:7544:1"
               ],
               "state": [
                  "finished"
               ],
               "start_time": [
                  1460980655
               ],
               "end_time": [
                  1460982464
               ],
               "user": [
                  "oid:user:id"
               ]
            }
         }
      ]
   }
}

```

But in my opinion this is not working correctly because this condition does not true:

```
 "should": [
                    {"bool": {
                        "must": [
                            {"term": {"state": {"value": "finished"}}}, 
                            {"range": {"start_time": {"lt": 1460930400}}}, 
                            {"range": {"end_time": {"gt": 1459461600}}}]
                        }}, 

```

and

"start\_time": [1460980655] \> 1460930400 and not "start\_time": [1460980655] \< 1460930400 how we specify in the query at the begining.

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 20, 2016, 9:23am UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/2 "2016-04-20T09:23:27Z")

</div>

Hi,

Clauses you specify under `should` don't necessarily have to match a doc for it to return in the result. A should-clause that matches will increase that documents score, so it will be ranked higher in the results. If the condition you are describing in the `should` clause needs to be true for all documents that are returned, it somehow needs to go into the `must`clause.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 20, 2016, 10:38am UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/3 "2016-04-20T10:38:07Z")

</div>

Look at minimum\_should\_match. The default is tricky, I believe it is 0 if  
there are must clauses and 1 if there aren't.

---

<div class="post-metadata">

**Author:** ![Juan\_Diaz\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Juan\_Diaz\_Gonzalez](https://discuss.elastic.co/u/Juan_Diaz_Gonzalez)\
**Post date:** [April 20, 2016, 11:12am UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/4 "2016-04-20T11:12:23Z")

</div>

At the end I changed my query, if before I have this:

A and (not B) and (C or D)

now with the new change I did:

(C and (A and (not B)) or (D and (A and (not B))

and in this way aparently works correctly. Finally my query is written so:

```
"query": {
        "bool": {
            "must": [
                
                {"term": {"user": {"value": "oid:user:id"}}},
                {"term": {"id": {"value": "oid:job:301:7544:1"}}}
               
                
                ], 
            "must_not": [
                {"term": {"state": {"value": "queued"}}}, 
                {"term": {"queue": {"value": "desktop.q"}}}], 
            "should": [
                {"bool": {
                    "must": [
                        {"term": {"state": {"value": "finished"}}}, 
                        {"range": {"start_time": {"lt": 1460930400}}}, 
                        {"range": {"end_time": {"gt": 1459461600}}}],

                     "must": [                
                        {"term": {"user": {"value": "oid:user:id"}}},
                       {"term": {"id": {"value": "oid:job:301:7544:1"}}}], 

                   "must_not": [
                        {"term": {"state": {"value": "queued"}}}, 
                       {"term": {"queue": {"value": "desktop.q"}}}]
                  }}, 
                {"bool": {
                    "must": [
                        {"term": {"state": {"value": "started"}}}, 
                        {"range": {"start_time": {"lt": 1460930400}}}]}}],

                  "must": [                
                        {"term": {"user": {"value": "oid:user:id"}}},
                       {"term": {"id": {"value": "oid:job:301:7544:1"}}}], 

                   "must_not": [
                        {"term": {"state": {"value": "queued"}}}, 
                       {"term": {"queue": {"value": "desktop.q"}}}]

                   
        }
    }
```

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [April 20, 2016, 1:26pm UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/5 "2016-04-20T13:26:49Z")

</div>

Thanks for sharing your results.

---

<div class="post-metadata">

**Author:** ![Juan\_Diaz\_Gonzalez](https://avatars.discourse-cdn.com/v4/letter/j/ea5d25/32.png) [@Juan\_Diaz\_Gonzalez](https://discuss.elastic.co/u/Juan_Diaz_Gonzalez)\
**Post date:** [April 20, 2016, 2:22pm UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/6 "2016-04-20T14:22:53Z")

</div>

Thanks to you

---

<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:57pm UTC](https://discuss.elastic.co/t/should-combining-bool-queries-does-not-works/47772/7 "2017-07-05T22:57:41Z")

</div>


