4

I have a bunch of documents with the fields username and device_os as follows:

{ "username": "foo", "device_os": "Android", ....},
{ "username": "foo", "device_os": "iOS", ....},
{ "username": "bar", "device_os": "Android", ....},
{ "username": "baz", "device_os": "iOS", ....},
{ "username": "foo", "device_os": "iOS", ....}

I would like to get all distinct device_os by username as follows:

{ 
  "foo": ["Android", "iOS"],
  "bar": ["Android"],
  "baz": ["iOS"] 
}

What is the best way to do something like this in elasticsearch, specifically elasticsearch-py?

Vineet Goel
  • 2,138
  • 1
  • 22
  • 28

2 Answers2

0

In case someone gets here, the idea to solve this problem is simple: use a second level aggregation on the usr_agg, something like this:

{
    "size": 0,
    "aggs": {
        "usr_agg": {
            "terms": {
                "field": "username.keyword"
            },
            "aggs": {
                "by_device_os": {
                    "terms": {
                        "field": "device_os.keyword"
                    }
                }
            }
        }
    }
}
andre
  • 448
  • 1
  • 3
  • 8
  • this seems not working, the nest aggs not found. `by_device_os` not found exception – tim Mar 15 '23 at 01:08
-1

you can use "terms" aggregation in following manner

{
 "query": {
           "match_all": {}
          },
 "aggs":{
          "usr_agg":{
                     "terms": {"field": "username"}
                    }
        }
 }

For more info