【发布时间】:2019-06-02 20:27:11
【问题描述】:
在 Kibana 中,我按如下方式创建索引:
PUT cars
{
"mappings":{
"_doc":{
"properties":{
"metadata":{
"type":"nested",
"properties":{
"str_value":{
"type":"keyword"
}
}
}
}
}
}
}
然后我插入三个记录:
POST /cars/_doc/1
{
"metadata": [
{
"key": "model",
"str_value": "Ford"
},
{
"key": "price",
"int_value": 1000
}
]
}
PUT /cars/_doc/2
{
"metadata": [
{
"key": "model",
"str_value": "Ford"
},
{
"key": "price",
"int_value": 2000
}
]
}
PUT /cars/_doc/3
{
"metadata": [
{
"key": "model",
"str_value": "Holden"
},
{
"key": "price",
"int_value": 2500
}
]
}
架构有点不合常规,但我已经这样设计索引以避免映射爆炸:
https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html
我想做的是获得我所有的汽车型号,以及这些型号的价格总和,即福特 3000 美元和霍顿 2500 美元。到目前为止,我有:
GET /cars/_search
{
"aggs":{
"metadata":{
"nested":{
"path":"metadata"
},
"aggs":{
"model_filter":{
"filter":{
"term":{
"metadata.key":"model"
}
},
"aggs":{
"model_counter":{
"terms":{
"field":"metadata.str_value",
"size":1000
}
}
}
}
}
}
}
}
这让我了解其中的一部分,因为它返回汽车型号和文档数量:
"aggregations": {
"metadata": {
"doc_count": 6,
"model_filter": {
"doc_count": 3,
"model_counter": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Ford",
"doc_count": 2
},
{
"key": "Holden",
"doc_count": 1
}
]
}
}
}
}
如何修改我的查询以添加一个子聚合,该子聚合将显示价格总和,即福特为 3000(两个文档的总和)和霍顿的 2500(一个文档的总和)
【问题讨论】:
标签: elasticsearch