# Top\_hits aggregation on a serial\_diff

**URL:** <https://discuss.elastic.co/t/top-hits-aggregation-on-a-serial-diff/50812>\
**Category:** Elasticsearch\
**Created:** [May 24, 2016, 9:03am UTC](https://discuss.elastic.co/t/top-hits-aggregation-on-a-serial-diff/50812 "2016-05-24T09:03:04Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephanbuys](https://avatars.discourse-cdn.com/v4/letter/s/a3d4f5/32.png) [@stephanbuys](https://discuss.elastic.co/u/stephanbuys)\
**Post date:** [May 24, 2016, 9:03am UTC](https://discuss.elastic.co/t/top-hits-aggregation-on-a-serial-diff/50812/1 "2016-05-24T09:03:04Z")

</div>

I've got a complex nested aggregation that uses a serial\_diff as final step to produce a new field. This field contains the information I want to visualise.

Unfortunately I can't get top\_hits to recognise the field created by serial\_diff, is there a way to specify that path or will I have to calculate the top\_hits myself on the client side? (Any pseudo code? 🙂 )

Thanks!

The query looks something like:  
POST /index/type/\_search  
{  
"size": 0,  
"query": {...  
},  
"aggs": {  
"agg1": {  
"terms": {  
"field": "field1.raw",  
"size": 0  
},  
"aggs": {  
"agg2": {  
"terms": {  
"field": "field2.raw",  
"size": 0  
},  
"aggs": {  
"timechart": {  
"date\_histogram": {  
...  
},  
"aggs": {  
"max\_field\_of\_interest": {  
"max": {  
"field": "field\_of\_interest"  
}  
},  
"usable\_value": {  
"serial\_diff": {  
"buckets\_path": "max\_field\_of\_interest",  
"lag": 1  
},  
"aggs": {  
"top\_results": {  
"top\_hits": {  
"sort": [  
{  
"usable\_value": {  
"order": "desc"  
}  
}  
],  
"size" : 10  
}  
...  
}

---

<div class="post-metadata">

**Author:** ![mpaduada](https://avatars.discourse-cdn.com/v4/letter/m/71c47a/32.png) [@mpaduada](https://discuss.elastic.co/u/mpaduada)\
**Post date:** [June 2, 2017, 2:18pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-on-a-serial-diff/50812/2 "2017-06-02T14:18:38Z")

</div>

The `top_hits` aggregation will return a subset of documents within the bucket. Your `usable_value` serial-differencing aggregation returns a metric based on the set of documents in the current bucket and in the immediately preceding bucket, which is to say that there is one value for `usable_value` per bucket, not per document. So, syntax aside, it doesn't make sense to sort documents in the bucket based on `usable_value`.

Can you clarify what you're trying to collect with your `top_hits` aggregation?

---

<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, 9:59pm UTC](https://discuss.elastic.co/t/top-hits-aggregation-on-a-serial-diff/50812/3 "2017-07-05T21:59:46Z")

</div>


