# Return all events between two events

**URL:** <https://discuss.elastic.co/t/return-all-events-between-two-events/161792>\
**Category:** Elasticsearch\
**Created:** [December 21, 2018, 6:51am UTC](https://discuss.elastic.co/t/return-all-events-between-two-events/161792 "2018-12-21T06:51:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rage5474](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rage5474/32/39085_2.png) [@rage5474](https://discuss.elastic.co/u/rage5474)\
**Post date:** [December 21, 2018, 6:51am UTC](https://discuss.elastic.co/t/return-all-events-between-two-events/161792/1 "2018-12-21T06:51:50Z")

</div>

I have a lot of log messages in one ElasticSearch index.  
These log messages containing two special messages. One start message and one end message (simply having a field with the value 'START' and 'STOP').

Can I create a query in ElasticSearch that returns all logs in that index between these two messages?

Here an example that shows simplified how my data look like:

```
PUT log
{
  "settings": {
    "index": {
      "number_of_shards": 1,
      "number_of_replicas": 0
    }
  },
  "mappings": {
    "_doc": {
      "properties": {
        "timestamp": {
          "type": "date",
          "format": "epoch_millis"
        },
        "msg": {
          "type": "text"
        }
      }
    }
  }
}

PUT log/_doc/1
{
    "timestamp": 1000,
    "msg" : "TO_EARLY"
}

PUT log/_doc/2
{
    "timestamp": 2000,
    "msg" : "START"
}

PUT log/_doc/3
{
    "timestamp": 3000,
    "msg" : "Hello, World!"
}

PUT log/_doc/4
{
    "timestamp": 4000,
    "msg" : "STOP"
}

PUT log/_doc/5
{
    "timestamp": 5000,
    "msg" : "TO_LATE"
}

```

So my question is: How does the query looks like to get the message between the START and the STOP message?

Thanks for your help.

Raphael

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 21, 2018, 9:41am UTC](https://discuss.elastic.co/t/return-all-events-between-two-events/161792/2 "2018-12-21T09:41:35Z")

</div>

I don't think you can do that easily. Documents are not "connected" together, meaning that they don't have any relationship.

What you should do is probably 2 queries. The first to get the min and max value (with min and max agg) for `timestamp` when `msg` is either `START` or `STOP`.  
Then use those 2 values as the input of the next query: a range query from min to max.

---

<div class="post-metadata">

**Author:** ![rage5474](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rage5474/32/39085_2.png) [@rage5474](https://discuss.elastic.co/u/rage5474)\
**Post date:** [December 21, 2018, 4:09pm UTC](https://discuss.elastic.co/t/return-all-events-between-two-events/161792/3 "2018-12-21T16:09:27Z")

</div>

Thanks for your answer.

I want to use this for a chart visualization in Kibana. Therefore I need only the values between the marker events. Is there a possibility to do this with two queries?

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 21, 2018, 6:11pm UTC](https://discuss.elastic.co/t/return-all-events-between-two-events/161792/4 "2018-12-21T18:11:27Z")

</div>

In Kibana? Then that's a question for #kibana forum I guess.  
I think it's not possible. It's always better anyway to compute everything you can at index time. Like computing a full event instead of small non related sub events...

---

<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:** [January 18, 2019, 6:11pm UTC](https://discuss.elastic.co/t/return-all-events-between-two-events/161792/5 "2019-01-18T18:11:27Z")

</div>

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