# Query technical question

**URL:** <https://discuss.elastic.co/t/query-technical-question/128187>\
**Category:** Elasticsearch\
**Created:** [April 16, 2018, 12:39pm UTC](https://discuss.elastic.co/t/query-technical-question/128187 "2018-04-16T12:39:45Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [April 16, 2018, 12:39pm UTC](https://discuss.elastic.co/t/query-technical-question/128187/1 "2018-04-16T12:39:45Z")

</div>

First the look of my fields.

- id
- parent\_id
- type

I want to write a query that show all the results that have this criteria :

- Each result's id shouldn't be a parent\_id of another result (parent\_id 0 doesn't count)
- The results should only be of type 1

For example :  
Id = 1, parent\_id = 11, type = 1  
Id = 2, parent\_id = 1, type = 2  
Id = 3, parent\_id = 4, type =2  
Id = 4, parent\_id = 7, type = 1  
Id = 5, parent\_id = 8, type = 2  
Id = 6, parent\_id = 9, type = 1  
Id = 7, parent\_id = 0, type = 3  
Id = 8, parent\_id = 0, type = 3  
Id = 9, parent\_id = 0, type = 3

So from these data, the result should give me the data of Id 6 only in this example

**In other words** , I want to verify before showing the result that each of the current data does not match a parent\_id of any other data.

**In other other words** , I want to put a must\_not id = parent\_id and must type = 1.

How to do this in elasticsearch?  
I hope my question is clear!

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 16, 2018, 7:12pm UTC](https://discuss.elastic.co/t/query-technical-question/128187/2 "2018-04-16T19:12:48Z")

</div>

You can filter by type using [term query](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-term-query.html) and you can use [has\_child](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-has-child-query.html) query wrapped into [boolean](https://www.elastic.co/guide/en/elasticsearch/reference/6.2/query-dsl-bool-query.html) must not to filter out records with children.

---

<div class="post-metadata">

**Author:** ![Islam\_Elshobokshy](https://avatars.discourse-cdn.com/v4/letter/i/77aa72/32.png) [@Islam\_Elshobokshy](https://discuss.elastic.co/u/Islam_Elshobokshy)\
**Post date:** [April 17, 2018, 7:09am UTC](https://discuss.elastic.co/t/query-technical-question/128187/3 "2018-04-17T07:09:33Z")

</div>

How to write it so that I would tell that the child shouldn't have a parent\_id of the parent's id? I would need to compare in the child all other data, and see if their parent\_id is equal to the id we're trying comparing them to. Your idea is good, but I can't seem to see how to compare them this way in ES?

I've been told to use scripts inside the child to do so, but that will be extremely slow to use scripts + has\_child. Is there another way to do that?

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [April 17, 2018, 1:47pm UTC](https://discuss.elastic.co/t/query-technical-question/128187/4 "2018-04-17T13:47:00Z")

</div>

> [@Islam\_Elshobokshy](#):
>
> I would need to compare in the child all other data, and see if their parent\_id is equal to the id we're trying comparing them to.

Isn't that what has\_child query is doing? If not I probably don't understand what you are trying to achieve. Sorry.

---

<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:** [May 15, 2018, 1:47pm UTC](https://discuss.elastic.co/t/query-technical-question/128187/5 "2018-05-15T13:47:01Z")

</div>

Closed on May 15, 2018, 1:47pm UTC

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