# Bool query retrun incorrect result when using "filter" and "should" in same time

**URL:** https://discuss.elastic.co/t/bool-query-retrun-incorrect-result-when-using-filter-and-should-in-same-time/52805
**Category:** Elasticsearch
**Created:** [June 15, 2016, 5:23am UTC](https://discuss.elastic.co/t/bool-query-retrun-incorrect-result-when-using-filter-and-should-in-same-time/52805 "2016-06-15T05:23:29Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Jin\_Guo](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jin\_Guo](https://discuss.elastic.co/u/Jin_Guo)
#### Post date: [June 15, 2016, 5:23am UTC](https://discuss.elastic.co/t/bool-query-retrun-incorrect-result-when-using-filter-and-should-in-same-time/52805/1 "2016-06-15T05:23:29Z")

</div>

Dear all,

My ES version is 2.3.3 and here is the sample index:

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "2",
        "_score": 1,
        "_source": {
          "age": "35",
          "name": "a",
          "text": "test"
        }
      },
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "age": "10",
          "name": "a",
          "text": "test"
        }
      },
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "3",
        "_score": 1,
        "_source": {
          "age": "15",
          "name": "a",
          "text": "test"
        }
      }
    ]
  }
}

```

and what I want query is something like " **name = a and (age \<=10 or age \> 20)**", the query I wrote like

```
GET _search
{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "name": "a"
          }
        }
      ],
      "should": [
        {
          "range": {
            "age": {
              "lte": "10"
            }
          }
        },
        {
          "range": {
            "age": {
              "gte": "20"
            }
          }
        }
      ]
    }
  }
}

```

But it got all result back

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.35355338,
    "hits": [
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "2",
        "_score": 0.35355338,
        "_source": {
          "age": "35",
          "name": "a",
          "text": "test"
        }
      },
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "1",
        "_score": 0.35355338,
        "_source": {
          "age": "10",
          "name": "a",
          "text": "test"
        }
      },
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "3",
        "_score": 0,
        "_source": {
          "age": "15",
          "name": "a",
          "text": "test"
        }
      }
    ]
  }
}

```

When I tried filtered query, it can return correct result:

```
GET _search
{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "filter": [
            {
              "term": {
                "name": "a"
              }
            }
          ],
          "should": [
            {
              "range": {
                "age": {
                  "lte": "10"
                }
              }
            },
            {
              "range": {
                "age": {
                  "gte": "20"
                }
              }
            }
          ]
        }
      }
    }
  }
}

```

the result is :

```
{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1,
    "hits": [
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "2",
        "_score": 1,
        "_source": {
          "age": "35",
          "name": "a",
          "text": "test"
        }
      },
      {
        "_index": "booltest",
        "_type": "test",
        "_id": "1",
        "_score": 1,
        "_source": {
          "age": "10",
          "name": "a",
          "text": "test"
        }
      }
    ]
  }
} 

```

So, did I do something wrong or a bug of bool query?

Thanks,

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [June 16, 2016, 9:14am UTC](https://discuss.elastic.co/t/bool-query-retrun-incorrect-result-when-using-filter-and-should-in-same-time/52805/2 "2016-06-16T09:14:37Z")

</div>

Hey,

have you tried putting all your criterias inside the `filter` part of a `bool` query, like this

```json
GET test/_search
{
  "query": {
    "bool": {
      "filter": {
        "bool": {
          "must" :{ "term" : { "name" : "a" } },
          "should" : [
           { "range" : { "age" : { "lte":10 } } },
           { "range" : { "age" : { "gte":20 } } }
          ]
        }
      }
    }
  }
}

```

That should do what you want?

--Alex

---

<div class="post-metadata">

### Author: ![Jin\_Guo](https://avatars.discourse-cdn.com/v4/letter/j/848f3c/32.png) [@Jin\_Guo](https://discuss.elastic.co/u/Jin_Guo)
#### Post date: [June 23, 2016, 5:49am UTC](https://discuss.elastic.co/t/bool-query-retrun-incorrect-result-when-using-filter-and-should-in-same-time/52805/3 "2016-06-23T05:49:58Z")

</div>

Alex,

You are genius! It works great.

Many thanks,

Jin

---

<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:41pm UTC](https://discuss.elastic.co/t/bool-query-retrun-incorrect-result-when-using-filter-and-should-in-same-time/52805/4 "2017-07-05T22:41:14Z")

</div>


