# Elasticsearch aggregation sort issue

**URL:** https://discuss.elastic.co/t/elasticsearch-aggregation-sort-issue/39432
**Category:** Elasticsearch
**Created:** [January 18, 2016, 10:01am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sort-issue/39432 "2016-01-18T10:01:47Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![akuzhan](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@akuzhan](https://discuss.elastic.co/u/akuzhan)
#### Post date: [January 18, 2016, 10:01am UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sort-issue/39432/1 "2016-01-18T10:01:47Z")

</div>

HI there,

elasticsearch is returning meaningless doc counts when we do aggregation sorting under certain circumstances. Issue is not there for 1.x and 2.x versions of elasticsearch.

Basically when I fire query below it returns 6 document for the specific link that I am looking for.

{  
"aggs": {  
"test": {  
"nested": {  
"path": "sharedLinks"  
},  
"aggs": {  
"aggssharedlinks": {  
"terms": {  
"field": "sharedLinks.sharedLink",  
"size": 1,  
"order": {  
"aggsfollowers\>x": "desc"  
}  
},  
"aggs": {  
"aggsfollowers": {  
"reverse\_nested": {

```
          },
          "aggs": {
            "x": {
              "sum": {
                "field": "authors.followers"
              }
            }
          }
        }
      }
    }
  }
}

```

}  
}

But when I add a specific term query for that url, it returns 33 document.

{  
"aggs": {  
"test": {  
"nested": {  
"path": "sharedLinks"  
},  
"aggs": {  
"aggssharedlinks": {  
"terms": {  
"field": "sharedLinks.sharedLink",  
"size": 1,  
"order": {  
"aggsfollowers\>x": "desc"  
}  
},  
"aggs": {  
"aggsfollowers": {  
"reverse\_nested": {

```
          },
          "aggs": {
            "x": {
              "sum": {
                "field": "authors.followers"
              }
            }
          }
        }
      }
    }
  }
}

```

}  
,  
"query": {  
"filtered": {  
"query": {  
"bool": {  
"must": [  
{  
"terms": {  
"sharedLinks.sharedLink": [  
"[http://www.abc.co.uk/1234542151551](http://www.abc.co.uk/1234542151551)"  
]  
}  
}  
]  
}  
}  
}  
}  
}

Here is my index structure;

{  
"IndexName": {  
"mappings": {  
"article": {  
"properties": {  
"articleId": {  
"type": "long",  
"doc\_values": true  
},  
"articleSource": {  
"properties": {  
"aggregationSourceName": {  
"type": "string",  
"index": "not\_analyzed",  
"doc\_values": true  
}  
}  
},  
"authors": {  
"properties": {  
"followers": {  
"type": "integer",  
"doc\_values": true  
}  
}  
},  
"sharedLinks": {  
"type": "nested",  
"include\_in\_parent": true,  
"properties": {  
"sharedLink": {  
"type": "string",  
"index": "not\_analyzed",  
"doc\_values": true  
}  
}  
}  
}  
}  
}  
}  
}

Do you have any idea what could be the issue ?

I can share data and queries via email if it helps.

Cheers!

---

<div class="post-metadata">

### Author: ![akuzhan](https://avatars.discourse-cdn.com/v4/letter/a/74df32/32.png) [@akuzhan](https://discuss.elastic.co/u/akuzhan)
#### Post date: [January 18, 2016, 4:11pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sort-issue/39432/2 "2016-01-18T16:11:23Z")

</div>

Looks like this is expected result. I was having 10 shards and when I say give me top 1, it was not returning enough documents.

I guess I am going to use custom routed shards and try to distinguish shards based on client Id on my case.

I just created an index with 1 single shard and fired same query again. All documents are in there and aggregation doc count is correct, it is all related to shard mechanism of elasticsearch.

---

<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: [July 5, 2017, 11:23pm UTC](https://discuss.elastic.co/t/elasticsearch-aggregation-sort-issue/39432/3 "2017-07-05T23:23:40Z")

</div>


