我无法弄清楚如何在复杂的嵌套对象上包装用于查询的“查询”语句。

我已经将概念简化为以下内容-

我有一个条目索引

{
"_index": "my_index",
"_type": "my_type",
"_id": "5",
"_source": {
  "group": "student",
  "user": [
    {
      "first": "Hubert",
      "last": "Rock",
      "grade": "B",
      "address": "12 Hunting St"
    }
  ]
}
}

其中“用户”是一个嵌套对象。现在,我要进行搜索以识别名字为“休伯特”的条目,
但在“成绩”和“地址”字段中都没有条目的人。

我可以单独执行此操作-
(获取所有“休伯特”)
GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            { "match": { "user.first": "Hubert" }}
          ]
        }
      }
    }
  }
}

(获取所有没有“等级”和“地址”值的条目)
GET my_index/_search
{
  "query": {
    "nested": {
      "path": "user",
      "query": {
       "bool": {
          "must_not": [
              {
                "exists" : {
                  "field":"user.grade"
                  }
              },
              {
                "exists" : {
                  "field":"user.address"
                  }
              }
          ]
        }
      }
    }
  }
}

但是我真的不知道如何将它们结合起来。有任何想法吗?

最佳答案

您需要做的是在单个 bool(boolean) 查询下结合mustmust_not子句,如下所示:

{
  "query": {
    "nested": {
      "path": "user",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "user.first": "Hubert"
              }
            }
          ],
          "must_not": [
            {
              "exists": {
                "field": "user.grade"
              }
            },
            {
              "exists": {
                "field": "user.address"
              }
            }
          ]
        }
      }
    }
  }
}

关于elasticsearch - Elasticsearch curl查询结合嵌套,存在,不存在查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55366661/

10-17 03:04
查看更多