简体   繁体   English

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

[英]Elasticsearch nested aggregation with nested object using NEST

I am trying to do an aggregation on a nested object. 我试图在嵌套对象上进行聚合。 The following is my json. 以下是我的json。 The first code sample below successfully returns the productCategory Id. 下面的第一个代码示例成功返回productCategory Id。 However, I want to return the category id and name in the aggregation. 但是,我想在聚合中返回类别ID和名称。 I thought I could try the second code sample below but it doesn't work. 我以为我可以尝试下面的第二个代码示例,但它不起作用。

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

This one aggregates the productCategory id as the key: 这个聚合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)
                    )
                )
            )
        )

But I need the category info, and this one doesn't work: 但我需要类别信息,这个不起作用:

        .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)
                    )
                )
            )
        )

If category is simply mapped as object , then the following will work 如果仅将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)
                )
            )
        )
    )
);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM