elasticsearch - Elastic-search 聚合 top 3 常见结果
问题描述
我的索引数据具有以下结构,我想聚合前 3 个重复次数最多的产品属性,因此前 3 个重复次数最多的产品属性将出现在聚合结果中
[
{
productProperty: "material",
productValue:[{value: wood},{value: plastic}] ,
},
{
productProperty: "material",
productValuea:[{value: wood},{value: plastic}] ,
},
{
productProperty: "type",
productValue:[{value: 26A},{value: 23A}] ,
},
{
productProperty: "type",
productValue:[{value: 22B},{value: 90C}] ,
},
{
productProperty: "material",
productValue:[{value: wood},{value: plastic}] ,
},
{
productProperty: "age_rating",
productValue:[{value: 18},{value: 13}] ,
}
]
下面的查询聚合所有基于 productProperty 但我怎样才能从中获得前 3 个结果
{
"query": {},
"aggs": {
"filtered_product_property": {
"filter": {
"bool": {
"must": []
}
},
"aggs": {
"aggs": {
"productProperty": {
"terms": {
"field": "productProperty"
}
}
}
}
}
}
}
解决方案
您可以在term aggregation
.
{
"query": {},
"aggs": {
"filtered_product_property": {
"filter": {
"bool": {
"must": []
}
},
"aggs": {
"aggs": {
"productProperty": {
"terms": {
"field": "productProperty",
"size" : 3
}
}
}
}
}
}
}
需要指出的是,在某些情况下,术语聚合并不是最准确的。
推荐阅读
- jaxb - 在 Apache Camel 中使用 JaxbDataFormat 进行编组
- javascript - Laravel 后端、Vue 前端、api 最佳实践?
- python - RotatingFileHandler 填充第一个文件后立即创建所有文件
- scala - 如何在 Spark SQL 中生成累积串联
- firebase-cloud-messaging - Firebase 云消息传递 (FCM) 心跳
- reactjs - 在 Netbeans 12 中,我可以在片段中添加文件名吗?
- json - JSON 编辑器清理数据集
- ios - Stripe Apple Pay 不会显示在带有 live 卡的主 iCloud 帐户上
- autodesk-forge - Forge Java SDK 与 Autodesk 客户端
- javascript - 为什么向按钮添加事件不起作用?