如何舍入双倍至2位小数点 - 弹性搜索 [英] How to round up double to 2 decimal point - elasticsearch

查看:164
本文介绍了如何舍入双倍至2位小数点 - 弹性搜索的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  {
entityFk:0,
entityCode:ADM,
entityObj:{
id:0,
code:ADM,
description ADM - FIRSTCOM
},
practiceFk:54745,
practiceObj:{
id:54745,
code:33.04。 01.32,
description:Artroopia com art tracamentia delesõesarticulares circunscritas,
practiceValue:23.5
}
}
} $ b $我想要将所有具有entityCode.description等于FIRST的practiceValue(not null)归结为所有,因此,我做了这个查询:

  {
size:0,
query:{
bool:{
must_not:[
{
missing:{field:practiceObj.practiceValue}
}
],
必须:[
{
match:{entityObj.description:FIRST}
}
]
}
},
aggs:{
total:{
sum:{field:practiceObj.practiceValue}

}
}
}

这是我获得的结果:

  {
take:26,
timed_out:false,
_shards:{
:5,
success:5,
failed:0
},
hits:{
total:11477,
max_score:0,
hits:[]
},
aggregations:{
total:{
value 1593598.7499999984
}
}
}

交易是:如何我将值舍入到2个小数点。
有人可以帮忙吗?
谢谢。



编辑:



这里是我的映射:

  {
index_practice_entities:{
mappings:{
practice_entities_search:{
property:{
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
}
}
}
}
}
}
}
}
pre>

解决方案

请尝试以下提及d脚本,它将累加值舍入到小数点后两位。

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

}
}


I have documents in elastic search that looks like :

{
                "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
                }
            }
        }

I want to sum all "practiceValue" (not null) that has entityCode.description equals to "FIRST", so I made this query :

    {
    "size" : 0,
    "query" : {
        "bool" : {
            "must_not" : [
                {
                    "missing" : { "field" : "practiceObj.practiceValue" }
                }
             ],
             "must" : [
                {
                    "match" : { "entityObj.description" : "FIRST" }
                }
             ]
         }
    },
    "aggs" : {
        "total" : {  
            "sum" : { "field" : "practiceObj.practiceValue"} 

        }
    }
}

Here is the result I obtained :

   {
    "took": 26,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 11477,
        "max_score": 0,
        "hits": []
    },
    "aggregations": {
        "total": {
            "value": 1593598.7499999984
        }
    }
}

The deal is: how can i round up the value to 2 decimal point. Can someone help? Thanks.

EDIT:

here´s my mapping :

 {
    "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"
                            }
                        }
                    }
                }
            }
        }
    }
}

解决方案

Please try the below mentioned script, It will round the aggregated value to 2 decimal places.

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

        }
    }

这篇关于如何舍入双倍至2位小数点 - 弹性搜索的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆