# elasticSearch 6.2 Query inside parent\_id

**URL:** https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025
**Category:** Elasticsearch
**Created:** [May 31, 2018, 10:40am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025 "2018-05-31T10:40:03Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![neelam.mamtani](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@neelam.mamtani](https://discuss.elastic.co/u/neelam.mamtani)
#### Post date: [May 31, 2018, 10:40am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/1 "2018-05-31T10:40:03Z")

</div>

Can anyone help how can I specify query inside parent\_id query to filter out children who has match in their parent docs.  
For e.g.  
This is my parent  
// "JOIN\_FIELD": "group",  
"GROUP\_MEMBER": [  
"1",  
"2",  
"3",  
"4",  
"5",  
"6",  
"7"  
]

and my child  
// "JOIN\_FIELD": {  
"name": "child",  
"parent": "1"  
}

Now I want to see child info a member say "3".  
// "query": {  
"bool": {  
"filter": [  
{  
"terms": {  
"GROUP\_MEMBER": [  
"3"  
]  
}  
}

but I have no clue how to proceed with getting all child of this parent selected. I have read has\_child supports query field but it is quite costly since it performs full join.

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [June 8, 2018, 7:41pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/2 "2018-06-08T19:41:58Z")

</div>

What elasticsearch, kibana, logstash versions are you using?  
Are all you configurations correct?

---

<div class="post-metadata">

### Author: ![neelam.mamtani](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@neelam.mamtani](https://discuss.elastic.co/u/neelam.mamtani)
#### Post date: [June 11, 2018, 6:22am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/3 "2018-06-11T06:22:10Z")

</div>

I am using elasticsearch 6.2.4. Yes my all queries run well. I wish to know how to have filtration on children inside parent\_id. Is there any way to do it currently or is it not at all supported?

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [June 13, 2018, 7:10pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/4 "2018-06-13T19:10:00Z")

</div>

Sorry, my previous answer was for some other question ( I have accidentally posted it here).

For your question - it is not very clear for me what you are trying to do. May be you can provide an expected output.

Do you mean that we you are running a `parent_id` query like this:

```auto

	"query": {
    "parent_id": {
			"type": "child",
			"id": "1"
		}
	}
}

```

you get a lot of children documents back for this parent with id=1, but you want only some of them?

Also, consider [has child](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-child-query.html) and [has parent](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-has-parent-query.html) query types.

---

<div class="post-metadata">

### Author: ![neelam.mamtani](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@neelam.mamtani](https://discuss.elastic.co/u/neelam.mamtani)
#### Post date: [June 14, 2018, 6:17am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/5 "2018-06-14T06:17:06Z")

</div>

This is exactly what I mean, I do not want all children but some which meet some filter criteria (which I do not understand where to specify).  
And this query will be used very frequently in my application and I believe hasParent and hasChild impact performance. Will this be a good decision to apply those queries?  
Can I use post\_filter here to filter out the children returned by parent\_id. Can you guide me if I am thinking in right direction?

---

<div class="post-metadata">

### Author: ![mayya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mayya/32/83147_2.png) [@mayya](https://discuss.elastic.co/u/mayya)
#### Post date: [June 14, 2018, 8:14pm UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/6 "2018-06-14T20:14:54Z")

</div>

Can you just combine these two queries in a boolean query, something like this:

```auto
{
	"query": {
		"bool" : {
			"filter": {
				"term" : { "<your field>" : "<your search term>" }
			},
			"must" : {
				"parent_id": { "type": "child", "id": "1"}
			}
		}
	}
}

```

---

<div class="post-metadata">

### Author: ![neelam.mamtani](https://avatars.discourse-cdn.com/v4/letter/n/71c47a/32.png) [@neelam.mamtani](https://discuss.elastic.co/u/neelam.mamtani)
#### Post date: [June 19, 2018, 11:43am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/7 "2018-06-19T11:43:50Z")

</div>

This sounds really cool. Thank you so much @mayya

---

<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: [July 17, 2018, 11:43am UTC](https://discuss.elastic.co/t/elasticsearch-6-2-query-inside-parent-id/134025/8 "2018-07-17T11:43:50Z")

</div>

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