# Query to match two separate events where one follows the other

**URL:** https://discuss.elastic.co/t/query-to-match-two-separate-events-where-one-follows-the-other/126018
**Category:** Elasticsearch
**Created:** [March 29, 2018, 5:36am UTC](https://discuss.elastic.co/t/query-to-match-two-separate-events-where-one-follows-the-other/126018 "2018-03-29T05:36:49Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Nikhil\_Utane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhil_utane/32/27547_2.png) [@Nikhil\_Utane](https://discuss.elastic.co/u/Nikhil_Utane)
#### Post date: [March 29, 2018, 5:36am UTC](https://discuss.elastic.co/t/query-to-match-two-separate-events-where-one-follows-the-other/126018/1 "2018-03-29T05:36:49Z")

</div>

Hi,  
This is what my team is currently doing for log analysis using grep & excel  
Step 1) Find all occurrences of logs that contain PATTERN1 OR PATTERN 2.  
Example output:  
t1: Log with PATTERN1  
t2: Log with PATTERN1  
t3: Log with PATTERN2  
t4: Log with PATTERN1

Step 2) Extract instances ONLY WHERE PATTERN2 follows PATTERN1  
From above output:  
t2: Log with PATTERN1  
t3: Log with PATTERN2

Step 3) Analyze other fields in "Log with PATTERN1" from the output of Step 2.

How can I achieve the same within Kibana? I suppose I'll have to use Query DSL. I am reading about joining queries and nested queries but some pointers will be helpful.

For the record, the logs do have an ID with which we can 'join' the two events but they are guaranteed to be unique only for a short duration after which they can be re-used.  
Also, the log with PATTERN2 has a timeout of 3 seconds. i.e. if it comes it will be within 3 seconds.

Appreciate any help.  
-Thanks  
Nikhil

---

<div class="post-metadata">

### Author: ![Nikhil\_Utane](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nikhil_utane/32/27547_2.png) [@Nikhil\_Utane](https://discuss.elastic.co/u/Nikhil_Utane)
#### Post date: [March 29, 2018, 5:39am UTC](https://discuss.elastic.co/t/query-to-match-two-separate-events-where-one-follows-the-other/126018/2 "2018-03-29T05:39:19Z")

</div>

I suppose another approach could be to use elapsed filter (or custom filter) and do it in logstash. Right?

---

<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: [April 26, 2018, 5:39am UTC](https://discuss.elastic.co/t/query-to-match-two-separate-events-where-one-follows-the-other/126018/3 "2018-04-26T05:39:44Z")

</div>

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