2

there is documents with category(number), and piece(long) fields. I need some kind of aggregration that group these docs by category and sum all the pieces in it

here how documents looks:

{
"category":"1",
"piece":"10.000"
},
{
"category":"2",
"piece":"15.000"
} ,
{
"category":"2",
"piece":"10.000"
},
{
"category":"3",
"piece":"5.000"
} 
...

The query result must be like:

{
"category":"1",
"total_amount":"10.000"
} 
{
"category":"2",
"total_amount":"25.000"
} 
{
"category":"3",
"total_amount":"5.000"
} 
..

any advice ?

1 Answer 1

1

You want to do a terms aggregation on the categories, from the records above I see that you are sending them as strings, so they will be categorical variables. Then, as a metric, pass on the sum.

This is how it might look:

        "aggs" : {
            "categories" : {
                "terms" : {
                    "field" : "category"
                },
                "aggs" : {
                    "sum_category" : {
                        "sum": { "field": "piece" } 
                    }
                }
            }
        }
Sign up to request clarification or add additional context in comments.

1 Comment

Yes thats exactly true :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.