【问题标题】:Combine nested query with filter将嵌套查询与过滤器相结合
【发布时间】:2016-06-09 17:22:59
【问题描述】:

我正在使用 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
          }
        }}]}
      }
    }
      }
  ]
  }}}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    以下查询应该有效。您不能将range 查询嵌套在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"
                      }
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    更新:

    在最新的 ES 版本中,上面的查询可以重写如下:

    {
      "size": 50,
      "query": {
        "bool": {
          "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": {
                  "term": {
                    "identities.identitytype": "2"
                  }
                }
              }
            }
          ]
        }
      }
    }
    

    【讨论】:

    猜你喜欢
    • 2011-07-25
    • 2019-08-11
    • 1970-01-01
    • 1970-01-01
    • 2014-11-14
    • 1970-01-01
    • 2021-08-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多