# Find consecutive field values in sorted collection of documents

**URL:** <https://discuss.elastic.co/t/find-consecutive-field-values-in-sorted-collection-of-documents/194943>\
**Category:** Elasticsearch\
**Created:** [August 13, 2019, 2:54am UTC](https://discuss.elastic.co/t/find-consecutive-field-values-in-sorted-collection-of-documents/194943 "2019-08-13T02:54:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zqd](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/zqd/32/41348_2.png) [@zqd](https://discuss.elastic.co/u/zqd)\
**Post date:** [August 13, 2019, 2:54am UTC](https://discuss.elastic.co/t/find-consecutive-field-values-in-sorted-collection-of-documents/194943/1 "2019-08-13T02:54:59Z")

</div>

I have documents such as the following that I can sorted by **id (sequence number)** or dateRecorded. Was sold is boolean. Using elasticsearch aggregations, how do I compute the longest consecutive **wasSold = true** or **wasSold = false** or some other field on the document, when I sort documents or filter with time frames.

```
{
  id: 1234343434,
  height: 12,
  width: 1,
  dateRecorded: "1565664751639",
  wasSold: true
}
```

---

<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 10, 2019, 2:55am UTC](https://discuss.elastic.co/t/find-consecutive-field-values-in-sorted-collection-of-documents/194943/2 "2019-09-10T02:55:01Z")

</div>

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