我在弹性搜索中有如下文档:
\n\n{\n "entityFk": 0,\n "entityCode": "ADM",\n "entityObj": {\n "id": 0,\n "code": "ADM",\n "description": "ADM - FIRSTCOM"\n },\n "practiceFk": 54745,\n "practiceObj": {\n "id": 54745,\n "code": "33.04.01.32",\n "description": "Artrotomia ou artroscopia com tratamento de les\xc3\xb5es articulares circunscritas ",\n "practiceValue": 23.5\n }\n }\n }\nRun Code Online (Sandbox Code Playgroud)\n\n我想对所有EntityCode.description等于“FIRST”的“practiceValue”(不为空)求和,所以我做了这个查询:
\n\n {\n "size" : 0,\n "query" : {\n "bool" : {\n "must_not" : [\n {\n "missing" : { "field" : "practiceObj.practiceValue" }\n }\n ],\n "must" : [\n {\n "match" : { "entityObj.description" : "FIRST" }\n }\n ]\n }\n },\n "aggs" : {\n "total" : { \n "sum" : { "field" : "practiceObj.practiceValue"} \n\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n这是我得到的结果:
\n\n {\n "took": 26,\n "timed_out": false,\n "_shards": {\n "total": 5,\n "successful": 5,\n "failed": 0\n },\n "hits": {\n "total": 11477,\n "max_score": 0,\n "hits": []\n },\n "aggregations": {\n "total": {\n "value": 1593598.7499999984\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n\n问题是:我怎样才能将值四舍五入到小数点后两位。\n有人可以帮忙吗?\n谢谢。
\n\n编辑:
\n\n这里\xc2\xb4s我的映射:
\n\n {\n "index_practice_entities": {\n "mappings": {\n "practice_entities_search": {\n "properties": {\n "entityCode": {\n "type": "string"\n },\n "entityFk": {\n "type": "long"\n },\n "entityObj": {\n "properties": {\n "code": {\n "type": "string"\n },\n "description": {\n "type": "string"\n },\n "id": {\n "type": "long"\n }\n }\n },\n "practiceFk": {\n "type": "long"\n },\n "practiceObj": {\n "properties": {\n "code": {\n "type": "string"\n },\n "description": {\n "type": "string"\n },\n "id": {\n "type": "long"\n },\n "practiceValue": {\n "type": "double"\n }\n }\n }\n }\n }\n }\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
请尝试下面提到的脚本,它会将聚合值四舍五入到小数点后两位。
"aggs" : {
"total" : {
"sum" : {
"script" : "Math.round(doc['practiceObj.practiceValue'].value*100)/100.0"
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
15536 次 |
| 最近记录: |