elasticsearch - Elasticsearch nested aggregation with nested object using NEST
问题描述
I am trying to do an aggregation on a nested object. The following is my json. The first code sample below successfully returns the productCategory Id. However, I want to return the category id and name in the aggregation. 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:
.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)
)
)
)
)
解决方案
如果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)
)
)
)
)
);
推荐阅读
- javascript - 如何使用 getElementsByTagName() 方法获取 body 标签的内容并将其存储在变量中
- android - android:/vendor 文件夹中的设备上没有剩余空间
- jpa - 保存列表
通过 jpa 没有创建新表 - javascript - 获取 innerHtml 或 innerText 或 nodeValue 并排除嵌套子级?
- python - 无法在 mojave 上为 mac 安装 mysqlclient
- machine-learning - 数据可视化和机器学习
- julia - LoadError:语法:从 untitled- 开始的表达式中出现意外的“结束”
- azure - StreamAnalytics Query:如何使用 if 和 else 语句编写查询?
- c# - 将 .Net Framework 从 3.5 升级到 4.6 时出错
- javascript - 当我从它创建一个无限循环自动提交时,我该如何停止