使用 NEST 嵌套对象的 Elasticsearch 嵌套聚合

Elasticsearch nested aggregation with nested object using NEST

我正在尝试对嵌套对象进行聚合。以下是我的json。下面的第一个代码示例成功 returns productCategory Id。但是,我想 return 聚合中的类别 ID 和名称。我想我可以尝试下面的第二个代码示例,但它不起作用。

"productCategories": [{
    "id":6,
    "productId":6,
    "categoryId":4,
    "category":{
        "parentId":2,
        "name":"Air Fresheners",
        "id":6
    }
}]

这个将 productCategory id 聚合为键:

        .Aggregations(aggs => aggs
            .Nested("agg-categories", nested => nested
                .Path(p => p.ProductCategories)
                .Aggregations(r => r
                    .Terms("agg-category", w => w
                        .Field(f => f.ProductCategories.First().Id)
                    )
                )
            )
        )

但是我需要分类信息,这个不行:

        .Aggregations(aggs => aggs
            .Nested("agg-categories", nested => nested
                .Path(p => p.ProductCategories.First().Category)
                .Aggregations(r => r
                    .Terms("agg-category", w => w
                        .Field(f => f.ProductCategories.First().Category.Id)
                    )
                )
            )
        )

如果 category 被简单地映射为 object,那么以下将起作用

var searchResponse = client.Search<Document>(s => s
    .Aggregations(aggs => aggs
        .Nested("agg-categories", nested => nested
            .Path(p => p.ProductCategories)
            .Aggregations(r => r
                .Terms("agg-category", w => w
                    .Field(f => f.ProductCategories.First().Category.Id)
                )
            )
        )
    )
);