在Elasticsearch中,如何在多级嵌套对象的多个字段中搜索字符串

问题描述 投票:1回答:1

在Elasticsearch 6中,我有嵌套对象的数据,如下所示:

{
    "brands" : 
    [
        {
            "brand_name" : "xyz",
            "products" : 
            [
               {
                   "title" : "test",
                   "mrp" : 100,
                   "sp" : 90,
                   "status" : 1
               },
               {
                   "title" : "test1",
                   "mrp" : 50,
                   "sp" : 45,
                   "status" : 1
                }
            ]
        },
        {
            "brand_name" : "aaa",
            "products" : 
            [
                {
                    "title" : "xyz",
                    "mrp" : 100,
                    "sp" : 90,
                    "status" : 1
                },
                {
                    "title" : "abc",
                    "mrp" : 50,
                    "sp" : 45,
                    "status" : 1
                }
            ]
        }
    ]
}

我想从字段brand_name或字段标题中搜索。我希望在相同的inner_hits中返回所有结果。

例如:如果我将搜索字符串输入为“xyz”,则应返回两个品牌对象和对应的产品对象。如果我将搜索字符串输入为“test”,则它应仅返回仅包含第一个产品对象的第一个品牌数组。

我怎样才能做到这一点。有任何想法吗?

我尝试过这样的嵌套路径查询:

{
  "query": {
    "bool": {
      "must": [
        {
          "nested": {
            "path": "brands",
            "query": {
              "bool": {
                "should": [
                  {
                    "term": {
                      "brands.brand_name": "xyz"
                    }
                  },
                  {
                    "term": {
                      "brands.brand_name.keyword": "aaa"
                    }
                  },
                  {
                    "nested": {
                      "path": "brands.products",
                      "query": {
                        "bool": {
                          "should": [
                            {
                              "match": {
                                "brands.products.title": "xyz"
                              }
                            }
                          ]
                        }
                      },
                      "inner_hits": {}
                    }
                  }
                ]
              }
            },
            "inner_hits": {}
          }
        }
      ]
    }
  }
}

但是这个查询返回多个inner_hits响应,每个品牌和每个产品都有多个数组对象。

我希望响应像所有与字符串匹配的品牌名称应列在一个数组下,所有产品应列在同一inner_hits下的另一个数组下。

elasticsearch nested multi-level elasticsearch-6
1个回答
1
投票

由于您希望内部匹配根据匹配发生的位置(即brands.brand_namebrands.products.title)而不同,因此您可以将两个查询用于品牌名称,将其他查询用于产品标题作为独立嵌套查询。这些查询应该在should查询的bool子句中。每个嵌套查询都应该有自己的inner_hits,如下所示:

{
  "query": {
    "bool": {
      "should": [
        {
          "nested": {
            "path": "brands",
            "inner_hits": {},
            "query": {
              "term": {
                "brands.brand_name.keyword": "test"
              }
            }
          }
        },
        {
          "nested": {
            "path": "brands.products",
            "inner_hits": {},
            "query": {
              "term": {
                "brands.products.title": "test"
              }
            }
          }
        }
      ]
    }
  },
  "_source": false
}
© www.soinside.com 2019 - 2024. All rights reserved.