elasticsearch 检查任何和/或所有嵌套对象是否存在字段

vhmi4jdf  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(153)

我看了一下ElasticSearch: search inside the array of objects,虽然它有帮助,但我实际上是在尝试确定是否至少有一个对象具有字段,以及是否所有嵌套对象都具有字段。
假装我们有一个所有冰箱的索引,上面有一个多余的文档,比如:

{
  "_id": "whatever",
  "location": "North Building 1",
  "floor": 2,
  "tag": "refrigerator-1",
  "contents" : [
    {
      "item": "milk-carton",
      "expires": 1-1-2023 
    },
    {
      "item": "pyrex-container",
    }
  ]
}

如何创建ElasticSearch查询:
1.查找至少有1件物品可能过期的冰箱( "exists" : { "field" : "expires" } }
1.查找没有过期物品的冰箱
1.查找所有物品都有过期字段的冰箱

odopli94

odopli941#

如果要在单个查询中执行此操作,请使用named_queries

查询

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "_name": "At least one expires",
            "path": "contents",
            "query": {
              "exists": {
                "field": "contents.expires"
              }
            }
          }
        },
        {
          "bool": {
            "_name": "None expires",
            "must_not": [
              {
                "nested": {
                  "path": "contents",
                  "query": {
                    "exists": {
                      "field": "contents.expires"
                    }
                  }
                }
              }
            ]
          }
        },
        {
          "bool": {
            "_name": "All expires",
            "must": [
              {
                "nested": {
                  "path": "contents",
                  "query": {
                    "exists": {
                      "field": "contents.expires"
                    }
                  }
                }
              }
            ],
            "must_not": [
              {
                "nested": {
                  "path": "contents",
                  "query": {
                    "bool": {
                      "must_not": [
                        {
                          "exists": {
                            "field": "contents.expires"
                          }
                        }
                      ]
                    }
                  }
                }
              }
            ]
          }
        }
      ]
    }
  }
}

结果

"hits" : [
      {
        "_index" : "index70",
        "_type" : "_doc",
        "_id" : "Qt2PVoQB_m3FhzcGBasD",
        "_score" : 2.0,
        "_source" : {
          "location" : "North Building 1",
          "floor" : 3,
          "tag" : "refrigerator-3",
          "contents" : [
            {
              "item" : "milk-carton",
              "expires" : "2023-01-01"
            },
            {
              "item" : "pyrex-container",
              "expires" : "2023-01-01"
            }
          ]
        },
        "matched_queries" : [
          "At least one expires",
          "All expires"
        ]
      },
      {
        "_index" : "index70",
        "_type" : "_doc",
        "_id" : "QN2BVoQB_m3FhzcG9qsG",
        "_score" : 1.0,
        "_source" : {
          "location" : "North Building 1",
          "floor" : 2,
          "tag" : "refrigerator-1",
          "contents" : [
            {
              "item" : "milk-carton",
              "expires" : "2023-01-01"
            },
            {
              "item" : "pyrex-container"
            }
          ]
        },
        "matched_queries" : [
          "At least one expires"
        ]
      },
      {
        "_index" : "index70",
        "_type" : "_doc",
        "_id" : "Qd2HVoQB_m3FhzcGUauO",
        "_score" : 0.0,
        "_source" : {
          "location" : "North Building 1",
          "floor" : 3,
          "tag" : "refrigerator-2",
          "contents" : [
            {
              "item" : "milk-carton"
            },
            {
              "item" : "pyrex-container"
            }
          ]
        },
        "matched_queries" : [
          "None expires"
        ]
      }
    ]

查询是自解释性的。如果要对三个条件使用单独的查询,请断开上面的查询。每个should子句将成为单独的查询

相关问题