如何使用嵌套搜索不在 Elasticsearch 中的对象中的数据?

How to search data that not in the object in Elasticsearch using Nested?

我尝试使用嵌套类型在 Elasticsearch 中搜索类别和产品。

我有这样的数据:

POST products/_doc
{
  "id" : 1,
  "category_id" : 1,
  "category_name" : "Test Category",
  "name": "Sapiente",
  "attributes" : [
    {
      "color" : "blue",
      "size" : 8
    },
    {
      "color" : "black",
      "size" : 16
    }
  ]
}

我尝试像这样搜索这些数据:

GET products/_search
{
  "query": {
    "nested": {
      "path": "attributes",
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "name.keyword": "Sapiente"
              }
            }
          ]
        }
      }
    }
  }
}

但我不明白如何搜索 NAME 或 CATEGORY_NAME 字段。 我只能搜索属性对象中的数据,否则我会得到空结果。

是否可以同时搜索 NAME、CATEGORY_NAME 和 ATTRIBUTES 对象中的数据?

提前谢谢你...

您可以使用 boolean query 组合嵌套和非嵌套查询

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "attributes",
            "query": {
              "bool": {
                "filter": [
                  {
                    "term": {
                      "attributes.color": "blue"
                    }
                  }
                ]
              }
            }
          }
        },
        {
          "term": {
            "name.keyword": "Sapiente"
          }
        }
      ]
    }
  }
}