Kibana to python dsl

I have this agg in kibana:
"aggs": {
"2": {
"terms": {
"field": "event_data.TargetUserName",
"size": 50,
"order": {
"_count": "desc"
}
}
}
}

I can get this far in python dsl
s.aggs.bucket('per_user', 'terms', field='event_data.TargetUserName', size=100)

How do I add the order option?

All the keyword arguments to the DSL methods are typically just converted to keys in the resulting dict/json code, this means you can just add order={'_count': 'desc'} like so: s.aggs.bucket(‘per_user’, ‘terms’, field=‘event_data.TargetUserName’, size=100, order={'_count': 'desc'})

Note however that that sorting (descending by number of documents) is the default so you don't need to specify anything :slight_smile:

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