# Detecting specific event sequences in data streams

**URL:** <https://discuss.elastic.co/t/detecting-specific-event-sequences-in-data-streams/342469>\
**Category:** Elasticsearch\
**Tags:** datastreams\
**Created:** [September 6, 2023, 7:40pm UTC](https://discuss.elastic.co/t/detecting-specific-event-sequences-in-data-streams/342469 "2023-09-06T19:40:13Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kargo](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kargo/32/125107_2.png) [@Kargo](https://discuss.elastic.co/u/Kargo)\
**Post date:** [September 6, 2023, 7:40pm UTC](https://discuss.elastic.co/t/detecting-specific-event-sequences-in-data-streams/342469/1 "2023-09-06T19:40:14Z")

</div>

Hey there.

I have a datastream in Elasticsearch with mappings similar to this:

```json
{
  "mappings": {
    "properties": {
      "@timestamp": {"type": "date"},  
      "event-type": {"type": "keyword"}, 
      "session": {"type": "keyword"},
      "message": {"type": "keyword"}
    }
  }
}

```

Given a sequence of documents like below, I need a term aggregation of the messages preceeding the "complete" event-type in the same session , which in this case would be `ipsum: 1` and `lorem: 1`

```json
{
  "event-type": "recieving", 
  "session": "1",
  "message": "lorem", 
},
{
  "event-type": "recieving", 
  "session": "1",
  "message": "ipsum", 
},
{
  "event-type": "complete", 
  "session": "1",
},
{
  "event-type": "recieving", 
  "session": "2",
  "message": "lorem", 
},
{
  "event-type": "recieving", 
  "session": "1",
  "message": "ipsum", 
}
{
  "event-type": "complete", 
  "session": "2",
}

```

Any ideas on how to do this within Elasticsearch? or would I need to retrieve all the events in the datastream for manual processing outside 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:** [October 4, 2023, 7:40pm UTC](https://discuss.elastic.co/t/detecting-specific-event-sequences-in-data-streams/342469/2 "2023-10-04T19:40:14Z")

</div>

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