Count unique fields with sum parameter range

Hi!!!
I have my data points where I have two fields:
user_id
buy_price

Each user_id can sent several data points(with different buy_price value).
Could you please help to count user_id's, grouped by sum their buy_prices(to 100, from 100 to 200, from 200).

Thanks for helping me!

If I understood your problem correctly, you wanted to have buckets based on buy_price and for each bucket count of user_id for this price.
Range aggregation should help you here.

GET /_search
{
    "aggs" : {
        "price_ranges" : {
            "range" : {
                "field" : "price",
                "ranges" : [
                    { "to" : 100.0 },
                    { "from" : 100.0, "to" : 200.0 },
                    { "from" : 200.0 }
                ]
            }
        }
    }
}

If this is not what you intended, please provide an example for the response you are looking for.

Thank's for your response!
Everything right, but it's not what I'm looking for. For example I have several docs:

user_id: Ben buy_price: 50
user_id: Ben buy_price: 100
user_id: Ben buy_price: 100
user_id: Jane buy_price: 50
user_id: Luke buy_price: 250

I need to count sum of their buy_price fields and count how many unique user_id each sum range has.
Response in this case should look like this:

to 100 - 1 (Jane. because sum of all her buys is equal 50)
from 100 to 200 - 0
from 200 - 2(Luke has one doc with price 250 and Ben has 3 docs (100+100+50))

Thanks for helping me.

I understand now what you intended to do. Unfortunately, you can not do this in elasticsearch.
This would involve having a pipeline range aggregation, which we have decided not to develop. You can read more here https://github.com/elastic/elasticsearch/issues/17590

Get it! Mayya, many thanks and good luck!!!)

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.