# Kibana to python dsl

**URL:** <https://discuss.elastic.co/t/kibana-to-python-dsl/98420>\
**Category:** Elasticsearch\
**Created:** [August 25, 2017, 9:05pm UTC](https://discuss.elastic.co/t/kibana-to-python-dsl/98420 "2017-08-25T21:05:02Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rugenl](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rugenl/32/12887_2.png) [@rugenl](https://discuss.elastic.co/u/rugenl)\
**Post date:** [August 25, 2017, 9:05pm UTC](https://discuss.elastic.co/t/kibana-to-python-dsl/98420/1 "2017-08-25T21:05:02Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![honzakral](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/honzakral/32/44958_2.png) [@honzakral](https://discuss.elastic.co/u/honzakral)\
**Post date:** [September 1, 2017, 1:33pm UTC](https://discuss.elastic.co/t/kibana-to-python-dsl/98420/2 "2017-09-01T13:33:51Z")

</div>

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 🙂

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [September 29, 2017, 1:34pm UTC](https://discuss.elastic.co/t/kibana-to-python-dsl/98420/3 "2017-09-29T13:34:14Z")

</div>

Closed on September 29, 2017, 1:34pm UTC

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