【问题标题】:Elasticsearch nested aggregation with nested object using NEST使用 NEST 嵌套对象的 Elasticsearch 嵌套聚合
【发布时间】:2019-04-26 07:19:05
【问题描述】:

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

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

这个聚合productCategory id作为key:

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

【问题讨论】:

    标签: elasticsearch nest


    【解决方案1】:

    如果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)
                    )
                )
            )
        )
    );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2015-11-18
      • 1970-01-01
      • 2016-10-14
      • 2016-03-27
      相关资源
      最近更新 更多