0

I have the following query where I want to query the indexname for ID "abc_12-def that fall within the date range specified in the range filter.

But the below query is fetching values of different ID as well(for eg: abc_12-edf, abc_12-pgf etc) and that fall outside the date range. Any advice on how I can give an AND condition here? Thanks.

curl -XPOST 'localhost:9200/indexname/status/_search?pretty=1&size=1000000' -d '{
"query": {
"filtered" : {
  "filter": [ 
    { "term":  { "ID": "abc_12-def" }}, 
    { "range": { "Date": { "gte": "2015-10-01T09:12:11", "lte" : "2015-11-18T10:10:13" }}} 
  ]
  }
  }
  }'
Sweet
  • 187
  • 3
  • 15

1 Answers1

0

You need to use Bool query for AND aka MUST condition

{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "ID": "abc_12-def"
          }
        },
        {
          "range": {
            "Date": {
              "gte": "2015-10-01T09:12:11",
              "lte": "2015-11-18T10:10:13"
            }
          }
        }
      ]
    }
  }
}

Also, all fields by default are analyzed using standard analyzer, which means abc_12-def is tokenized as [abc_12, def]. term query does not analyze the string.

If you are looking for an exact match, you should mark the field as not_analyzed. How to map it as not_analyzed is explained here.

Community
  • 1
  • 1
Rahul
  • 15,979
  • 4
  • 42
  • 63