简体   繁体   中英

How to perform AND condition in elasticsearch query?

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" }}} 
  ]
  }
  }
  }'

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM