title: How to sort bucket result based on viewed_timestamp in ElasticSearch?
body: <p>I am new to Elastic Search. I want to find the top 10 unique recent visited doc_id.</p>
  
  <p>I have done first aggregation on doc_id and added sub-aggregation to sort each group and get a single result.  Now I want to sort this bucket.
  I am not able to sort the bucket's result based on view_timestamp. How can I add order during first aggregation? </p>
  
  <p>I have tried other solutions given on stack overflow, but it is not working for me. Can anyone help me to solve this problem? </p>
  
  <hr>
  
  <p>Query</p>
  
  <pre><code>{
      "query": {
          "constant_score": {
              "filter": {
                  "term": { "username": "nil@gmail.com" }
              }
          }
      },
  
      "size":0,
      "aggs":{
          "title": {
              "terms": {
                  "field": "doc_id",
                  "size":0
              }
              ,
              "aggs": {
                  "top": {
                      "top_hits": {
                         "sort": [
                          {
                              "viewed_timestamp": {
                                  "order": "desc"
                              }
                          }
                      ],
                          "size": 1
  
                      }
                  }
              }        
  
          }
  
      }    
  
  }
  </code></pre>
  
  <hr>
  
  <p>Bucket result:</p>
  
  <pre><code>{
      "aggregations": {
          "title": {
              "doc_count_error_upper_bound": 0,
              "sum_other_doc_count": 0,
              "buckets": [{
                  "key": "b003",
                  "doc_count": 3,
                  "top_tag_hits": {
                      "hits": {
                          "total": 3,
                          "max_score": null,
                          "hits": [{
                              "_index": "visitedData",
                              "_type": "userdoc",
                              "_id": "AVak51Sp",
                              "_score": null,
                              "_source": {
                                  "viewed_timestamp": "20160819T152359",
                                  "content_type": "bp",
                                  "title": "Data print",
                                  "doc_id": "BP003"
                              },
                              "sort": [
                                  1471620239000
                              ]
                          }]
                      }
                  }
              }, {
                  "key": "bp004",
                  "doc_count": 3,
                  "top_tag_hits": {
                      "hits": {
                          "total": 3,
                          "max_score": null,
                          "hits": [{
                              "_index": "visitedData",
                              "_type": "userdoc",
                              "_id": "AVak513Y8G",
                              "_score": null,
                              "_source": {
                                  "viewed_timestamp": "20160819T152401",
                                  "content_type": "bp",
                                  "title": "Application Print",
                                  "doc_id": "BP004"
                              },
                              "sort": [
                                  1471620241000
                              ]
                          }]
                      }
                  }
              }]
          }
      }
  }
  </code></pre>