简体   繁体   English

如何在Elasticsearch查询中执行AND条件?

[英]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. 我有以下查询,我想在该查询中查询属于范围过滤器中指定日期范围内的ID“ abc_12-def”的索引名。

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. 但是以下查询也正在获取不同ID的值(例如abc_12-edf,abc_12-pgf等),并且这些值超出了日期范围。 Any advice on how I can give an AND condition here? 关于如何在此处给出AND条件的任何建议? 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 您需要使用布尔查询AND又名MUST调理

{
  "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]. 另外,默认情况下,所有字段都使用标准分析器进行分析 ,这意味着abc_12-def被标记为[abc_12,def]。 term query does not analyze the string. term query不分析字符串。

If you are looking for an exact match, you should mark the field as not_analyzed . 如果您正在寻找完全匹配的内容,则应将该字段标记为not_analyzed How to map it as not_analyzed is explained here. 此处说明了如何将其映射为not_analyzed

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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