php - Elasticsearch - 按字段获取结果摘要
问题描述
{"name": "John", "district": 1, "light": 2},
{"name": "John", "district": 5, "light": 2},
{"name": "John", "district": 5, "light": 27},
{"name": "John", "district": 11, "light": 1},
{"name": "John", "district": 11, "light": 0},
{"name": "John", "district": 11, "light": 10},
{"name": "John", "district": 8, "light": 6},
{"name": "Mary", "district": 2, "light": 10},
{"name": "Nick", "district": 1, "light": 0},
{"name": "Bob", "district": 3, "light": 10},
{"name": "Kenny", "district": 1, "light": 8}
按名称字段搜索时,如何按组数获取剩余字段的摘要?
例子:
搜索时
"query": {
"term": {
"name": {
"value": "john"
}
}
}
预期输出:
{
"district": {
11: 3, // value : the amount of matches
5: 2,
1: 1,
},
"light": {
2: 2, // value : the amount of matches
27: 1,
10: 1,
// etc..
}
}
解决方案
尝试一对术语聚合:
{
"size": 0,
"query": {
"term": {
"name": {
"value": "john"
}
}
},
"aggs": {
"by_district": {
"terms": {
"field": "district"
}
},
"by_light": {
"terms": {
"field": "light"
}
}
}
}
推荐阅读
- bash - 在 WSL bash 脚本中将目录更改为 Window 的用户目录
- node.js - 如何用 Homebrew 的节点替换我的节点版本?
- c++ - 如何对对的向量进行排序
按第一个和第二个值? - python - 正则表达式匹配包含月份的日期范围
- sql - 获取两列,一列包含分组值,另一列不包含分组值
- javascript - 切换 TAB 时未调用 statusChanges
- python - Pandas 中的箱线图:带聚合的分组箱线图
- batch-file - 如何根据每个文件名中的文件夹名称将文件移动到现有文件夹?
- python - Python排序嵌套字典
- hockeyapp - 使用 API(令牌)从应用中心/曲棍球应用下载最新的工件