Elasticsearch从嵌套集合中返回所有字段

zpgglvta  于 2022-11-22  发布在  ElasticSearch
关注(0)|答案(1)|浏览(139)

我查询elasticsearch嵌套字段,并在资源中只得到嵌套字段类别。这段代码适用于没有嵌套字段。

aggs: {
        categories: {
            nested: {
                path: "categories"
            },
            aggs: {
                category_name: {
                    terms: {
                        field: "categories.categoryId.keyword",
                        min_doc_count: 1,
                        size: 500,
                        order: {
                            _count: "desc"
                        },
                    },
                    aggs: {
                        product: {
                            top_hits: {
                                size: 1,
                                _source: {
                                    include: [
                                        "id", "assets", "categories"
                                    ]
                                }
                            }
                        }

                    }
                }
            }
        }
    }
dly7yett

dly7yett1#

嵌套上下文无法访问父字段。需要使用reverse_nested aggregation

GET index65/_search
{
  "size": 0,
  "aggs": {
    "categories": {
      "nested": {
        "path": "categories"
      },
      "aggs": {
        "category_name": {
          "terms": {
            "field": "categories.categoryId.keyword",
            "min_doc_count": 1,
            "size": 500,
            "order": {
              "_count": "desc"
            }
          },
          "aggs": {
            "product": {
              "reverse_nested": {},
              "aggs": {
                "NAME": {
                  "top_hits": {
                    "size": 1,
                    "_source": {
                      "include": [
                       "id", "assets", "categories"
                      ]
                    }
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

相关问题