# Bool query with must query is matching all documents when the query doesn't match

**URL:** <https://discuss.elastic.co/t/bool-query-with-must-query-is-matching-all-documents-when-the-query-doesnt-match/139406>\
**Category:** Elasticsearch\
**Created:** [July 10, 2018, 5:19pm UTC](https://discuss.elastic.co/t/bool-query-with-must-query-is-matching-all-documents-when-the-query-doesnt-match/139406 "2018-07-10T17:19:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Santu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santu/32/45072_2.png) [@Santu](https://discuss.elastic.co/u/Santu)\
**Post date:** [July 10, 2018, 5:19pm UTC](https://discuss.elastic.co/t/bool-query-with-must-query-is-matching-all-documents-when-the-query-doesnt-match/139406/1 "2018-07-10T17:19:58Z")

</div>

Hi,

i am using 5.x version of elasticsearch. my loaded documents are:

POST index1/\_bulk  
{ "index" : {"\_type" : "type1", "\_id" : "1" } }  
{"name": "sahana","rollno": "123456","class": "first"}  
{ "index" : {"\_type" : "type1", "\_id" : "2" } }  
{"name": "sahana","rollno": "654321","class": "first"}

and my query having must operator query:

"query": {"bool":{"must":[{"term":{"name":"sahana"}},{"term":{"rollno":"123456"}}]}}

and my get request on above documents:  
GET index1/\_search

i am seeing an issue where if a query, such as a simple\_query\_string used in must clause of a bool query, and the analyzed query string results in an empty query string, then the it behaves like the entire query is ignored and all documents are matched. Even if the simple\_query\_string. query is inside a filtered query, then the filters are ignored. i am expecting the document with id 1 only for this.

any ideas or help on this would be much appreciated.

Note: i am also tried with query\_string query, but still able to get the all the documents

---

<div class="post-metadata">

**Author:** ![Santu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/santu/32/45072_2.png) [@Santu](https://discuss.elastic.co/u/Santu)\
**Post date:** [July 11, 2018, 11:57am UTC](https://discuss.elastic.co/t/bool-query-with-must-query-is-matching-all-documents-when-the-query-doesnt-match/139406/2 "2018-07-11T11:57:23Z")

</div>

as you can see i use the simple\_query\_string query. but i dont know somehow its still returning the both 2 docs.

```
{
"query" : {
  "bool" : {
    "must" : [
      {
        "match_all" : {
          "boost" : 1.0
        }
      }
    ],
    "filter" : [
      {
        "bool" : {
          "must" : [
            {
              "indices" : {
                "indices" : [
                  "index1"
                ],
                "query" : {
                  "simple_query_string" : {
                    "query" : "{\"bool\":{\"must\":[{\"term\":{\"name\":\"sahana\"}},{\"term\":{\"rollno\":\"123456\"}}]}}",
                    "flags" : -1,
                    "default_operator" : "or",
                    "lenient" : false,
                    "analyze_wildcard" : false,
                    "boost" : 1.0
                  }
                },
                "no_match_query" : {
                  "match_all" : {
                    "boost" : 1.0
                  }
                },
                "boost" : 1.0
              }
            }
          ],
          "disable_coord" : false,
          "adjust_pure_negative" : true,
          "boost" : 1.0
        }
      }
    ],
    "disable_coord" : false,
    "adjust_pure_negative" : true,
    "boost" : 1.0
  }
},
  "ext" : { }
}
```

---

<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:** [August 8, 2018, 11:57am UTC](https://discuss.elastic.co/t/bool-query-with-must-query-is-matching-all-documents-when-the-query-doesnt-match/139406/3 "2018-08-08T11:57:24Z")

</div>

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