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

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,

Hey,

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

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

Alex,

You are genius! It works great.

Many thanks,

Jin