Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams "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"
         "sum":{
            "field":"price",
            "script":"BigDecimal.valueOf(_value).setScale(4, RoundingMode.HALF_UP)",
            "missing":0

In this example you can manage the scale changing .setScale to 2.

Hello Manuel, I know you've answered this ages ago, but you're my last glimpse of hope. Can you take a look at this thread and tell me if the problem is solvable? This seems right in your ballpark. stackoverflow.com/questions/73658743/… – Keperoze Sep 10, 2022 at 12:02
 "aggs" : {
    "total" : {  
        "sum" : { "script" : "(doc['practiceObj.practiceValue'].value).round(2)"  // practiceValue should be double or float

Make sure to to enable scripting

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.