# Help with complex DSL query/filter/grouping

**URL:** https://discuss.elastic.co/t/help-with-complex-dsl-query-filter-grouping/282608
**Category:** Kibana
**Created:** [August 26, 2021, 7:21pm UTC](https://discuss.elastic.co/t/help-with-complex-dsl-query-filter-grouping/282608 "2021-08-26T19:21:43Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Bryan\_Hamilton](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bryan_hamilton/32/82111_2.png) [@Bryan\_Hamilton](https://discuss.elastic.co/u/Bryan_Hamilton)
#### Post date: [August 26, 2021, 7:21pm UTC](https://discuss.elastic.co/t/help-with-complex-dsl-query-filter-grouping/282608/1 "2021-08-26T19:21:43Z")

</div>

Hi all,  
I have 2 seperate queries and am trying to find a way to filter and only return documents matching both queries.

Query01 = `event.provider : "Microsoft-Windows-Sysmon" and event.code:"3" and destination.port:( "389" or "636")`

Query02 = `event.provider : "Microsoft-Windows-Sysmon" and winlog.event_id: "18"`

Even though documents returned by these 2 queries have diffent field sets, they all have at least 2 fields in common: the `process.executable` and the `process.pid`. To illustrate this, if I combine these 2 queries with an **or** statement, the result would look something like this:

 ![Aggregation issue](https://us1.discourse-cdn.com/elastic/original/3X/f/b/fb9c32e2e8aaaf90f49bf51c644b0fc25a1bb5bb.png)

Now, I would like to use either the `process.executable` or the `process.pid` or both as a reference and only display documents with a `process.executable` found both in query01 and query02. So, ultimately every `process.executable` displayed will have at least one document with `event.code = 3` and at least one other document with `event event.code = 18`.

My research so far indicates that I might be able to acheive this by using maybe compound or scripted query/filter in DSL but I haven't been able to find any exemple anywhere that would give me something start from.

Any help would be appreciated.

Thanks

---

<div class="post-metadata">

### Author: ![tsullivan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/tsullivan/32/31077_2.png) [@tsullivan](https://discuss.elastic.co/u/tsullivan)
#### Post date: [August 30, 2021, 8:37pm UTC](https://discuss.elastic.co/t/help-with-complex-dsl-query-filter-grouping/282608/2 "2021-08-30T20:37:11Z")

</div>

Hi, this use case is very similar to a join, which isn't supported in Elasticsearch. Your best option would be to index the raw data into an entity-centric index that combines related data into a single document.

---

<div class="post-metadata">

### Author: ![Bryan\_Hamilton](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/bryan_hamilton/32/82111_2.png) [@Bryan\_Hamilton](https://discuss.elastic.co/u/Bryan_Hamilton)
#### Post date: [September 8, 2021, 7:31am UTC](https://discuss.elastic.co/t/help-with-complex-dsl-query-filter-grouping/282608/3 "2021-09-08T07:31:47Z")

</div>

Thanks @tsullivan , I will try doing that.

---

<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 6, 2021, 7:32am UTC](https://discuss.elastic.co/t/help-with-complex-dsl-query-filter-grouping/282608/4 "2021-10-06T07:32:27Z")

</div>

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