【发布时间】:2015-10-02 04:38:59
【问题描述】:
我只需要对匹配查询的嵌套对象的值求和。看起来 ElasticSearch 确定与查询匹配的文档,然后对所有嵌套对象求和。从下面的大纲中,我想搜索 nestedobjects.objtype="A" 并仅返回匹配嵌套对象的 objvalue 的总和,我想获得值 4。这可能吗?如果有,怎么做?
这是映射
{
"myindex": {
"mappings": {
"mytype": {
"properties": {
"nestedobjects": {
"type": "nested",
"include_in_parent": true,
"properties": {
"objtype": {
"type": "string"
},
"objvalue": {
"type": "integer"
}
}
}
}
}
}
}
}
这是我的文件
PUT /myindex/mytype/1
{
"nestedobjects": [
{ "objtype": "A", "objvalue": 1 },
{ "objtype": "B", "objvalue": 2 }
]
}
PUT /myindex/mytype/2
{
"nestedobjects": [
{ "objtype": "A", "objvalue": 3 },
{ "objtype": "B", "objvalue": 3 }
]
}
这是我的查询代码。
POST allscriptshl7/_search?search_type=count
{
"query": {
"filtered": {
"query": {
"query_string": {
"query": "nestedobjects.objtype:A"
}
}
}
},
"aggregations": {
"my_agg": {
"sum": {
"field": "nestedobjects.objvalue"
}
}
}
}
【问题讨论】:
标签: elasticsearch