# Time/Order based query

**URL:** https://discuss.elastic.co/t/time-order-based-query/33731
**Category:** Elasticsearch
**Created:** [November 4, 2015, 11:24am UTC](https://discuss.elastic.co/t/time-order-based-query/33731 "2015-11-04T11:24:33Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![John2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@John2](https://discuss.elastic.co/u/John2)
#### Post date: [November 4, 2015, 11:24am UTC](https://discuss.elastic.co/t/time-order-based-query/33731/1 "2015-11-04T11:24:33Z")

</div>

I am using elasticsearch to import log events. Suppose an event X occurs, followed by event Y. Is there a query that can be performed that will find all documents that match an X event followed by a Y event using the Query DSL? Could aggregations be used, or is this kind of query not supported in elasticsearch?

Thanks.

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [November 4, 2015, 1:56pm UTC](https://discuss.elastic.co/t/time-order-based-query/33731/2 "2015-11-04T13:56:28Z")

</div>

At the moment, these types of queries are tough for Elasticsearch. Event X may be located on one shard, while Event Y may be on a different shard. Matching/sorting based on sequential causality would mean that both shards (on potentially different nodes) would have to coordinate their actions and communicate, which could be very expensive.

You _might_ be able to accomplish something similar with the new pipeline aggregations, but not likely. These aggs work on the results of other aggregations (e.g. they operate on buckets, not documents), so you'd only be able to calculate stats on the sampled buckets.

You'll probably have better luck by designing some kind of "entity-centric indexing" scheme, where you save the sequential relationship in an "entity" and use that document to determine matches. Mark Harwood has a few presentations on the subject:

- [https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes](https://www.elastic.co/elasticon/2015/sf/building-entity-centric-indexes)
- [https://www.elastic.co/videos/entity-centric-indexing-london-meetup-sep-2014](https://www.elastic.co/videos/entity-centric-indexing-london-meetup-sep-2014)
- [https://www.youtube.com/watch?v=yBf7oeJKH2Y](https://www.youtube.com/watch?v=yBf7oeJKH2Y)

---

<div class="post-metadata">

### Author: ![John2](https://avatars.discourse-cdn.com/v4/letter/j/f0a364/32.png) [@John2](https://discuss.elastic.co/u/John2)
#### Post date: [November 12, 2015, 1:29pm UTC](https://discuss.elastic.co/t/time-order-based-query/33731/3 "2015-11-12T13:29:43Z")

</div>

Thanks for your reply. I had a feeling that it wouldn't be easy, if at all possible.

---

<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:38pm UTC](https://discuss.elastic.co/t/time-order-based-query/33731/4 "2017-07-05T23:38:58Z")

</div>


