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



我看了看ElasticSearch:对象数组内的搜索,虽然它有帮助,但我实际上试图确定是否至少有一个字段,如果所有嵌套的对象都有字段。

假设我们有一个包含多余文档的所有冰箱的索引,如:

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

如何创建一个弹性搜索查询;

  1. 查找至少有一种物品可以过期的冰箱( "exists" : { "field" : "expires" } }
  2. 查找没有过期物品的冰箱
  3. 查找所有物品都有过期字段的冰箱

如果您想在单个查询中完成此操作,请使用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子句将成为一个单独的查询

相关内容

  • 没有找到相关文章

最新更新