【问题标题】:elastic exists query for nested documents嵌套文档的弹性存在查询
【发布时间】:2017-05-20 09:45:16
【问题描述】:

我有一个嵌套文档:

"someField": "hello",
"users": [
   {
     "name": "John",
      "surname": "Doe",
      "age": 2
   }
]

根据这个https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html,上面应该匹配:

GET /_search
{
  "query": {
    "exists" : { "field" : "users" }
  }

}

而以下不应该,

"someField": "hello",
"users": []

但不幸的是两者都不匹配。有什么想法吗?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    来自user3775217 的答案对我有用,但我需要对其进行调整以按预期为must_not 工作。本质上,bool/must 需要包裹在查询的 nested 部分:

    {
        "query": {
            "bool": {
                "must": [
                    {
                        "nested": {
                            "path": "users",
                            "query": {
                                "exists": {
                                    "field": "users"
                                }
                            }
                        }
                    }
                }
            ]
        }
    }
    

    【讨论】:

      【解决方案2】:

      Elasticsearch 博客中提到的示例是指字符串和字符串类型的数组,而不是嵌套类型。

      以下查询应该适合您:

      {
          "query": {
              "nested": {
                  "path": "users",
                  "query": {
                      "bool": {
                          "must": [
                              {
                                  "exists": {
                                      "field": "users"
                                  }
                              }
                          ]
                      }
                  }
              }
          }
      }
      

      另外,您可以参考this issue 了解更多信息,其中讨论了这种使用模式。

      【讨论】:

      • 任何人都知道为什么嵌套字段 DSL 必须设计得如此复杂。
      • 此查询为我返回空结果。虽然在发现模式下我看到具有非空嵌套字段的文档。帮忙?
      【解决方案3】:

      这对我有用

      GET /type/_search?pretty=true
      {
        "query": {
          "bool": {
            "must": [
              {
                "nested": {
                  "path": "outcome",
                  "query": {
                    "exists": {
                      "field": "outcome.outcomeName"
                    }
                  }
                }
              }
            ]
          }
        }
      }
      

      【讨论】:

        【解决方案4】:

        具有以下索引映射:

        {
          "index_name": {
              "mappings": {
                "object_name": {
                    "dynamic": "strict",
                    "properties": {
                      "nested_field_name": {
                          "type": "nested",
                          "properties": {
                            "some_property": {
                                "type": "keyword"
                            }
                          }
                      }
                    }
                }
              }
          }
        }
        

        我需要使用这个查询:

        GET /index_name/_search
        {
          "query": {
              "nested": {
                "path": "nested_field_name",
                "query": {
                    "bool": {
                      "must": [
                          {
                            "exists": {
                                "field": "nested_field_name.some_property"
                            }
                          }
                      ]
                    }
                }
              }
          }
        }
        

        Elasticsearch 5.4.3 版

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-12-25
          • 2023-01-10
          • 2018-03-09
          • 2021-09-01
          • 2015-09-24
          • 2019-09-12
          • 2021-02-25
          • 2016-08-16
          相关资源
          最近更新 更多