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 ?

4

1 回答 1

1

您想对类别进行术语聚合,从上面的记录中我看到您将它们作为字符串发送,因此它们将是分类变量。然后,作为一个指标,传递总和。

这可能是这样的:

        "aggs" : {
            "categories" : {
                "terms" : {
                    "field" : "category"
                },
                "aggs" : {
                    "sum_category" : {
                        "sum": { "field": "piece" } 
                    }
                }
            }
        }
于 2015-10-05T20:39:23.703 回答