【问题标题】:How to search data that not in the object in Elasticsearch using Nested?如何使用嵌套在 Elasticsearch 中搜索不在对象中的数据?
【发布时间】:2021-06-13 21:04:47
【问题描述】:

我尝试使用嵌套类型在 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 对象中的数据?

提前谢谢你...

【问题讨论】:

    标签: object elasticsearch search nested


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2011-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多