我有一个在我开始开发之前设计的数据模型。模型具有键值对的集合(它位于嵌套对象中)。我正在尝试过滤此集合上的特定键,如下所示:
{
"created":"2015-09-07",
"collection":[
{
"key":"a",
"value":12
},
{
"key":"b",
"value":21
},
{
"key":"c",
"value":36
}
]
}
例如:我正在尝试获取其键为“a”的值的总和,我尝试了如下查询
{
"aggs": {
"allMembers": {
"filter": {
"term": {
"doc.collection.key": "a"
}
},
"aggs": {
"rev": {
"sum": {
"field": "doc.collection.value"
}
}
}
}
},
"query": {}
}
此查询返回每个文档中所有值的总和。
我认为这是正常的,目前的数据模型无法做到这一点。我搜索了类似的案例,但找不到。我认为 ElasticSearch 并不打算这样做。但其他团队成员并不相信。
我想也许我应该在程序中执行此操作,将整个文档转换为另一个对象并通过查询获取集合的总和。但在这种情况下,我为什么要使用弹性搜索,我可以使用 nosql 数据库或 redis 来做到这一点。
总和聚合器中的脚本字段可以帮助我吗?有没有办法在当前数据模型的弹性搜索中做到这一点?或者我应该在程序上做吗?
文件的映射
{
"smyrna": {
"mappings": {
"_default_": {
"_source": {
"includes": [
"meta.*"
]
},
"properties": {
"meta": {
"type": "object",
"include_in_all": false
}
}
},
"couchbaseDocument": {
"_source": {
"includes": [
"meta.*",
"doc.*"
]
},
"properties": {
"doc": {
"properties": {
"body": {
"properties": {
"data": {
"type": "nested",
"properties": {
"products": {
"properties": {
"categories": {
"properties": {
"categoryId": {
"type": "long"
},
"categoryName": {
"type": "string"
}
}
},
"currency": {
"type": "string"
},
"endQuantity": {
"type": "long"
},
"itemCode": {
"type": "string"
},
"modelCode": {
"type": "long"
},
"modelName": {
"type": "string",
"analyzer": "keyword"
},
"name": {
"type": "string"
},
"quantity": {
"type": "long"
},
"totalPrice": {
"type": "long"
},
"trademark": {
"properties": {
"manufacturerName": {
"type": "string"
},
"trademarkName": {
"type": "string"
}
}
},
"unitPrice": {
"type": "long"
}
}
},
"totalAmount": {
"type": "long"
},
"uri": {
"type": "string"
},
"userId": {
"type": "string"
}
}
}
}
},
"created": {
"type": "date",
"format": "dateOptionalTime"
},
"header": {
"properties": {
"appKey": {
"type": "string"
},
"channel": {
"type": "string"
},
"client": {
"type": "string"
}
}
},
"id": {
"type": "string"
},
"new in 2.0": {
"type": "string"
},
"ownerAge": {
"type": "long"
}
}
}
}
}
}
}
}
实际上我正在尝试计算每个 modelCode 的总和