# Elasticsearch sort query

**URL:** <https://discuss.elastic.co/t/elasticsearch-sort-query/189790>\
**Category:** Elasticsearch\
**Created:** [July 10, 2019, 2:08pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790 "2019-07-10T14:08:02Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [July 10, 2019, 2:08pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/1 "2019-07-10T14:08:02Z")

</div>

I have some data that I want to sort but when I sort them sort query didn't work very well. So, here what I got after making my query:

```auto
"Group3": {
                      "buckets": [
                        {
                          "doc_count": 8, 
                          "key": "1"
                        }, 
                        {
                          "doc_count": 1, 
                          "key": "11"
                        }, 
                        {
                          "doc_count": 1, 
                          "key": "15"
                        }, 
                        {
                          "doc_count": 1, 
                          "key": "18"
                        }, 
                        {
                          "doc_count": 1, 
                          "key": "8"
                        }
                      ], 
                      "doc_count_error_upper_bound": 0, 
                      "sum_other_doc_count": 0
                    }, 
                    "doc_count": 12, 
                    "key": "mke"

```

This my query:

```auto
   "aggs": {
                    "Group1": {
                        "terms": {
                            "field": "method.keyword",
                            "include": ".*POST.*",
                        },
                        "aggs": {
                            "Group2": {
                                "terms": {
                                    "field": "keyword.keyword",
                                    "size": 11593,

                                },
                                "aggs": {
                                    "Group3": {
                                        "terms": {
                                            "field": "amount.keyword",
                                            "size": 100,

                                        }
                                    }

                                },
                            }
                        },

                    }

                }

```

What I want is output need to show me the top key value among them, like this:

```auto
"Group3": {
                      "buckets": [
                        {
                          "doc_count": 1, 
                          "key": "18"
                        }
                  
                      ], 
                      "doc_count_error_upper_bound": 0, 
                      "sum_other_doc_count": 0
                    }, 
                    "doc_count": 12, 
                    "key": "mke"

```

How can I do that

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 10, 2019, 3:20pm UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/2 "2019-07-10T15:20:23Z")

</div>

Please format your code, logs or configuration files using `</>` icon as explained in [this guide](https://discuss.elastic.co/t/about-the-elasticsearch-category/21) and not the citation button. It will make your post more readable.

Or use markdown style like:

````
```
CODE
```

````

This is the icon to use if you are not using markdown format:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/7/e/7e6e239431ec2d71cbf1beef741f2e93e7cc762c.jpg)

There's a live preview panel for exactly this reasons.

Lots of people read these forums, and many of them will simply skip over a post that is difficult to read, because it's just too large an investment of their time to try and follow a wall of badly formatted text.  
If your goal is to get an answer to your questions, it's in your interest to make it as easy to read and understand as possible.  
Please update your post.

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [July 11, 2019, 6:07am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/3 "2019-07-11T06:07:54Z")

</div>

Thank you for warning me sir. I am new here and I didn't give attention to make my question easy to understand. I guess I formatted my question well right now.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 11, 2019, 7:12am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/4 "2019-07-11T07:12:29Z")

</div>

Much appreciated. Thanks.

Could you also answer the last part of my answer about a full reproduction script?

---

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [July 11, 2019, 7:27am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/5 "2019-07-11T07:27:42Z")

</div>

I am sorry sir. I couldn't understand what you want from me with full reproduction script.  
I am using elasticsearch for python and this one is my code. Hope this is the thing that you want:

```auto
class Page(Resource):
    def get(self):
        body = {
            "aggs": {
                "Group1": {
                    "terms": {
                        "field": "method.keyword",
                        "include": ".*POST.*",
                    },
                    "aggs": {
                        "Group2": {
                            "terms": {
                                "field": "keyword.keyword",
                                "size": 11593,

                            },
                            "aggs": {
                                "Group3": {
                                    "terms": {
                                        "field": "amount.keyword",
                                        "size": 100,

                                    }
                                }

                            },
                        }
                    },

                }

            }

        }

        res=es.search(index='EStry',body=body)
        return jsonify(res)

```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 11, 2019, 8:09am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/6 "2019-07-11T08:09:03Z")

</div>

What don't you understand in the page I linked to?

---

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [July 11, 2019, 8:19am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/7 "2019-07-11T08:19:48Z")

</div>

It seems to me like I did all the thing correct. It confuses me. In the page you linked queries has written for Kibana user. I am using Python and I develop RESTAPI. It seems different than my situation.  
Pardon me.

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 11, 2019, 8:30am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/8 "2019-07-11T08:30:48Z")

</div>

Yes please provide a script that anyone can run in Kibana.

---

<div class="post-metadata">

**Author:** ![Ahmet\_Kartal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ahmet_kartal/32/47196_2.png) [@Ahmet\_Kartal](https://discuss.elastic.co/u/Ahmet_Kartal)\
**Post date:** [July 11, 2019, 8:38am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/9 "2019-07-11T08:38:18Z")

</div>

Hope this is what you want sir:

```
GET EStry/_search
        {            
        "aggs": {
                    "Group1": {
                        "terms": {
                            "field": "method.keyword",
                            "include": ".*POST.*",
                        },
                        "aggs": {
                            "Group2": {
                                "terms": {
                                    "field": "keyword.keyword",
                                    "size": 11593,

                                },
                                "aggs": {
                                    "Group3": {
                                        "terms": {
                                            "field": "amount.keyword",
                                            "size": 100,

                                        }
                                    }

                                },
                            }
                        },

                    }

                }
        }
```

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [July 11, 2019, 8:51am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/10 "2019-07-11T08:51:36Z")

</div>

If I run that in Kibana it will tell me that Index `EStry` is not found.

---

<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:** [August 8, 2019, 8:56am UTC](https://discuss.elastic.co/t/elasticsearch-sort-query/189790/11 "2019-08-08T08:56:58Z")

</div>

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