将嵌套查询与过滤器结合使用

时间:2016-02-26 13:11:57

标签: elasticsearch

我正在使用ES 2.1并具有以下映射:

"startDate": {
  "type": "date", 
  "format": "yyyy-MM-dd HH:mm:ss:SSS||yyyy-MM-dd HH:mm:ss", 
  "index": "not_analyzed", 
  "store": true
},"identities": {
  "type": "nested",
  "properties": {
    "identityatt": { "type": "integer", "index": "not_analyzed", "store": true },
    "identitykey": { "type": "string", "index": "not_analyzed", "store": true },
    "identityval": { "type": "string", "index": "not_analyzed", "store": true },
    "identitytype": { "type": "integer", "index": "not_analyzed", "store": true }
  }
}

以下查询很好,它们会返回我的期望:

{ "size": 50,
  "query": {
    "filtered": {
      "filter": {
        "range": {
          "startDate": {
            "from": "2016-02-19 11:11:25",
            "to": "2016-02-27 11:11:25",
            "include_lower": true,
            "include_upper": true
          }
}}}}}

这个过滤了一个时间范围,下一个我希望用特殊的身份类型

检索所有
{
  "size": 50,
  "query": {
    "nested": {
      "path": "identities",
      "filter": {
        "term": {
          "identities.identitytype": "2"
        }
}}}}

但我似乎没有得到将这两者结合起来的查询。

我尝试将时间范围查询添加到嵌套的过滤器中,将两个过滤器嵌套到bool过滤器中,我也尝试使用filtered查询,但没有运气将两者结合起来。

查看https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html处的示例,它还包含范围查询,但区别在于它位于嵌套对象中,而我的startDate未包含在嵌套对象中。

关于如何组合这些查询的任何想法?

修改

我还尝试了这里提出的建议:Combined non-Nested and Nested Query in Elasticsearch并收到错误"No query registered for [filter]"

{
  "size": 50,
  "query": {
  "bool": {
  "must": [
  {"filter": {
        "range": {
          "startDate": {
            "from": "2016-02-19 11:11:25",
            "to": "2016-02-27 11:11:25",
            "include_lower": true,
            "include_upper": true
          }
        }
      }},
      {"nested": {
      "path": "identities",
      "filter": { "bool": { "must": [{
        "term": {
          "identities.identitytype": "2"
        },
        "range": {
          "startDate": {
            "from": "2016-02-19 11:11:25",
            "to": "2016-02-27 11:11:25",
            "include_lower": true,
            "include_upper": true
          }
        }}]}
      }
    }
      }
  ]
  }}}

1 个答案:

答案 0 :(得分:2)

以下查询应该有效。您无法将nested查询嵌套在bool/must内,您需要将其保留在{ "size": 50, "query": { "filtered": { "filter": { "bool": { "must": [ { "range": { "startDate": { "from": "2016-02-19 11:11:25", "to": "2016-02-27 11:11:25", "include_lower": true, "include_upper": true } } }, { "nested": { "path": "identities", "filter": { "term": { "identities.identitytype": "2" } } } } ] } } } } } 的同一级别。

task newZip(type: Zip)<<{
    basename newZip
    from ziptree(oldZip.zip)
    from filetree(dir/of/new/files)
}