我有 flex 搜索中的文档,看起来像:

{
                "entityFk": 0,
                "entityCode": "ADM",
                "entityObj": {
                    "id": 0,
                    "code": "ADM",
                    "description": "ADM - FIRSTCOM"
                },
                "practiceFk": 54745,
                "practiceObj": {
                    "id": 54745,
                    "code": "33.04.01.32",
                    "description": "Artrotomia ou artroscopia com tratamento de lesões articulares circunscritas  ",
                    "practiceValue": 23.5
                }
            }
        }

我想对所有具有attributeCode.description等于“FIRST”的“practiceValue”(不为null)求和,因此我进行了以下查询:
    {
    "size" : 0,
    "query" : {
        "bool" : {
            "must_not" : [
                {
                    "missing" : { "field" : "practiceObj.practiceValue" }
                }
             ],
             "must" : [
                {
                    "match" : { "entityObj.description" : "FIRST" }
                }
             ]
         }
    },
    "aggs" : {
        "total" : {
            "sum" : { "field" : "practiceObj.practiceValue"}

        }
    }
}

这是我获得的结果:
   {
    "took": 26,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 11477,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "total": {
            "value": 1593598.7499999984
        }
    }
}

达成的协议(protocol)是:如何将值四舍五入到小数点后两位。
有人可以帮忙吗?
谢谢。

编辑:

这是我的映射:
 {
    "index_practice_entities": {
        "mappings": {
            "practice_entities_search": {
                "properties": {
                    "entityCode": {
                        "type": "string"
                    },
                    "entityFk": {
                        "type": "long"
                    },
                    "entityObj": {
                        "properties": {
                            "code": {
                                "type": "string"
                            },
                            "description": {
                                "type": "string"
                            },
                            "id": {
                                "type": "long"
                            }
                        }
                    },
                    "practiceFk": {
                        "type": "long"
                    },
                    "practiceObj": {
                        "properties": {
                            "code": {
                                "type": "string"
                            },
                            "description": {
                                "type": "string"
                            },
                            "id": {
                                "type": "long"
                            },
                            "practiceValue": {
                                "type": "double"
                            }
                        }
                    }
                }
            }
        }
    }
}

最佳答案

请尝试以下提到的脚本,它将合计的值四舍五入到小数点后两位。

"aggs" : {
        "total" : {
            "sum" : {
              "script" : "Math.round(doc['practiceObj.practiceValue'].value*100)/100.0"
               }

        }
    }

关于elasticsearch - 如何四舍五入到小数点后两位至小数点-ElasticSearch,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/36041812/

10-13 07:50