# Term query to get children based on parent id

**URL:** <https://discuss.elastic.co/t/term-query-to-get-children-based-on-parent-id/16296>\
**Category:** Elasticsearch\
**Created:** [March 11, 2014, 7:20pm UTC](https://discuss.elastic.co/t/term-query-to-get-children-based-on-parent-id/16296 "2014-03-11T19:20:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rukshan\_Perera](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rukshan\_Perera](https://discuss.elastic.co/u/Rukshan_Perera)\
**Post date:** [March 11, 2014, 7:20pm UTC](https://discuss.elastic.co/t/term-query-to-get-children-based-on-parent-id/16296/1 "2014-03-11T19:20:04Z")

</div>

Hello,

I have uploaded curl scripts to gist  
[https://gist.github.com/rukshanperera/9492610](https://gist.github.com/rukshanperera/9492610).

I have a three level parent/child types as shown in the following mappings

{ "Parent" :  
{  
"properties" : {  
"name" : {  
"type" :"string",  
"index":"analyzed"  
}  
}  
}  
}

{  
"Child" :{  
"\_parent": {  
"type": "Parent"  
},  
"properties" : {  
"name" : {  
"type" :"string",  
"index":"analyzed"  
}  
}  
}  
}

{  
"GrandChild" :{  
"\_parent": {  
"type": "Child"  
},  
"properties" : {  
"name" : {  
"type" :"string",  
"index":"analyzed"  
}  
}  
}  
}

I want to search for \*GrandChild \*documents based on its parent id (which  
is the id of a Child in this example). I used the following query but it  
does not return any hits. ( "bob" - id of \*Child \*document. "alice" - id  
of \*Parent \*document)

curl -s -XPOST "[http://localhost:9200/myindex/GrandChild/\_search?routing=alice](http://localhost:9200/myindex/GrandChild/_search?routing=alice)" -d '  
{  
"query" : {  
"term" : { "\_parent" : "bob" }  
}  
}'

However, I was able to get \*Child \*documents based on \*Parent \*id using the  
following ( "alice" - id of \*Parent \*document)

curl -s -XPOST "[http://localhost:9200/myindex/Child/\_search?routing=alice](http://localhost:9200/myindex/Child/_search?routing=alice)" -d '  
{  
"query" : {  
"term" : { "\_parent" : "alice" }  
}

}

Can someone please help me to get the first query working?

Thanks,  
Rukshan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Clinton\_Gormley](https://avatars.discourse-cdn.com/v4/letter/c/50afbb/32.png) [@Clinton\_Gormley](https://discuss.elastic.co/u/Clinton_Gormley)\
**Post date:** [March 12, 2014, 10:31am UTC](https://discuss.elastic.co/t/term-query-to-get-children-based-on-parent-id/16296/2 "2014-03-12T10:31:19Z")

</div>

Hi Rukshan

Very nicely laid out question. Thanks for providing all of the steps.

I agree that it doesn't work and (at least according to the docs) it  
should, so I've opened an issue here:

> <https://github.com/elastic/elasticsearch/issues/5399>
>
> With a Parent \> Child \> Grandchild relationship, you can use a \`term\` query on t…he \`\_parent\` field to find children with a particular \`$parent\`, but not grandchildren with parent \`$Child\`:
> 
> \`\`\`
> DELETE /myindx
> 
> PUT /myindex
> {
> "mappings": {
> "Parent": {
> "properties": {
> "name": {
> "type": "string",
> "index": "analyzed"
> }
> }
> },
> "Child": {
> "\_parent": {
> "type": "Parent"
> },
> "properties": {
> "name": {
> "type": "string",
> "index": "analyzed"
> }
> }
> },
> "GrandChild": {
> "\_parent": {
> "type": "Child"
> },
> "properties": {
> "name": {
> "type": "string",
> "index": "analyzed"
> }
> }
> }
> }
> }
> 
> 
> POST /\_bulk 
> { "index" : { "\_index" : "myindex", "\_type" : "Parent", "\_id" : "alice" } }
> { "name" : "Alice" }
> { "index" : { "\_index" : "myindex", "\_type" : "Child", "\_id" : "bob", "parent" : "alice" } }
> { "name" : "Bob"}
> { "index" : { "\_index" : "myindex", "\_type" : "GrandChild", "\_id" : "gc1", "parent" : "bob", "routing" : "alice" } }
> { "name" : "grand child 1" }
> { "index" : { "\_index" : "myindex", "\_type" : "GrandChild", "\_id" : "gc2", "parent" : "bob", "routing" : "alice" } }
> { "name" : "grand child 2"}
> 
> GET /\_search?fields=\_parent,\_routing
> \`\`\`
> 
> This query works:
> 
> \`\`\`
> POST /myindex/Child/\_search
> {
> "query": {
> "term": {
> "\_parent": "alice"
> }
> }
> }
> \`\`\`
> 
> This query doesn't:
> 
> \`\`\`
> POST /myindex/GrandChild/\_search
> {
> "query": {
> "term": {
> "\_parent": "bob"
> }
> }
> }
> \`\`\`
> 
> But performing the same lookup with \`has\_parent\` does work:
> 
> \`\`\`
> POST /myindex/GrandChild/\_search
> {
> "query": {
> "has\_parent": {
> "parent\_type": "Child",
> "query": {
> "filtered": {
> "filter": {
> "term": {
> "\_id": "bob"
> }
> }
> }
> }
> }
> }
> }
> \`\`\`

Curiously, performing the same lookup using a `has_parent` query does work:

POST /myindex/GrandChild/\_search  
{  
"query": {  
"has\_parent": {  
"parent\_type": "Child",  
"query": {  
"filtered": {  
"filter": {  
"term": {  
"\_id": "bob"  
}  
}  
}  
}  
}  
}  
}

Clint

On 11 March 2014 20:20, Rukshan Perera [rukshanperera@gmail.com](mailto:rukshanperera@gmail.com) wrote:

> Hello,
> 
> I have uploaded curl scripts to gist  
> [Term query to get children using parent id · GitHub](https://gist.github.com/rukshanperera/9492610).
> 
> I have a three level parent/child types as shown in the following mappings
> 
> { "Parent" :  
> {  
> "properties" : {  
> "name" : {  
> "type" :"string",  
> "index":"analyzed"  
> }  
> }  
> }  
> }
> 
> {  
> "Child" :{  
> "\_parent": {  
> "type": "Parent"  
> },  
> "properties" : {  
> "name" : {  
> "type" :"string",  
> "index":"analyzed"  
> }  
> }  
> }  
> }
> 
> {  
> "GrandChild" :{  
> "\_parent": {  
> "type": "Child"  
> },  
> "properties" : {  
> "name" : {  
> "type" :"string",  
> "index":"analyzed"  
> }  
> }  
> }  
> }
> 
> I want to search for \*GrandChild \*documents based on its parent id (which  
> is the id of a Child in this example). I used the following query but it  
> does not return any hits. ( "bob" - id of \*Child \*document. "alice" - id  
> of \*Parent \*document)
> 
> curl -s -XPOST "[http://localhost:9200/myindex/GrandChild/\_search?routing=alice](http://localhost:9200/myindex/GrandChild/_search?routing=alice)" -d '  
> {  
> "query" : {  
> "term" : { "\_parent" : "bob" }  
> }  
> }'
> 
> However, I was able to get \*Child \*documents based on \*Parent \*id using  
> the following ( "alice" - id of \*Parent \*document)
> 
> curl -s -XPOST "[http://localhost:9200/myindex/Child/\_search?routing=alice](http://localhost:9200/myindex/Child/_search?routing=alice)" -d '  
> {  
> "query" : {  
> "term" : { "\_parent" : "alice" }  
> }
> 
> }
> 
> Can someone please help me to get the first query working?
> 
> Thanks,  
> Rukshan
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAPt3XKQ5uOmhddsLBr%3D7hb%2BbMKGMtGng-FiyGUW3ps3%2B0UCtBA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAPt3XKQ5uOmhddsLBr%3D7hb%2BbMKGMtGng-FiyGUW3ps3%2B0UCtBA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Rukshan\_Perera](https://avatars.discourse-cdn.com/v4/letter/r/bb73d2/32.png) [@Rukshan\_Perera](https://discuss.elastic.co/u/Rukshan_Perera)\
**Post date:** [March 12, 2014, 4:42pm UTC](https://discuss.elastic.co/t/term-query-to-get-children-based-on-parent-id/16296/3 "2014-03-12T16:42:29Z")

</div>

Hi Clint,

Thanks for the answer. 'has\_parent' query you have suggested works and I  
should be able to use it in my application.

Regards,  
Rukshan

On Wednesday, March 12, 2014 3:31:19 AM UTC-7, Clinton Gormley wrote:

> Hi Rukshan
> 
> Very nicely laid out question. Thanks for providing all of the steps.
> 
> I agree that it doesn't work and (at least according to the docs) it  
> should, so I've opened an issue here:  
> [Term query for \_parent doesn't work for grandchild · Issue #5399 · elastic/elasticsearch · GitHub](https://github.com/elasticsearch/elasticsearch/issues/5399)
> 
> Curiously, performing the same lookup using a `has_parent` query does work:
> 
> POST /myindex/GrandChild/\_search  
> {  
> "query": {  
> "has\_parent": {  
> "parent\_type": "Child",  
> "query": {  
> "filtered": {  
> "filter": {  
> "term": {  
> "\_id": "bob"  
> }  
> }  
> }  
> }  
> }  
> }  
> }
> 
> Clint
> 
> On 11 March 2014 20:20, Rukshan Perera \<[ruksha...@gmail.com](mailto:ruksha...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hello,
> > 
> > I have uploaded curl scripts to gist  
> > [Term query to get children using parent id · GitHub](https://gist.github.com/rukshanperera/9492610).
> > 
> > I have a three level parent/child types as shown in the following mappings
> > 
> > { "Parent" :  
> > {  
> > "properties" : {  
> > "name" : {  
> > "type" :"string",  
> > "index":"analyzed"  
> > }  
> > }  
> > }  
> > }
> > 
> > {  
> > "Child" :{  
> > "\_parent": {  
> > "type": "Parent"  
> > },  
> > "properties" : {  
> > "name" : {  
> > "type" :"string",  
> > "index":"analyzed"  
> > }  
> > }  
> > }  
> > }
> > 
> > {  
> > "GrandChild" :{  
> > "\_parent": {  
> > "type": "Child"  
> > },  
> > "properties" : {  
> > "name" : {  
> > "type" :"string",  
> > "index":"analyzed"  
> > }  
> > }  
> > }  
> > }
> > 
> > I want to search for \*GrandChild \*documents based on its parent id  
> > (which is the id of a Child in this example). I used the following query  
> > but it does not return any hits. ( "bob" - id of \*Child \*document.  
> > "alice" - id of \*Parent \*document)
> > 
> > curl -s -XPOST "[http://localhost:9200/myindex/GrandChild/\_search?routing=alice](http://localhost:9200/myindex/GrandChild/_search?routing=alice)" -d '  
> > {  
> > "query" : {  
> > "term" : { "\_parent" : "bob" }  
> > }  
> > }'
> > 
> > However, I was able to get \*Child \*documents based on \*Parent \*id using  
> > the following ( "alice" - id of \*Parent \*document)
> > 
> > curl -s -XPOST "[http://localhost:9200/myindex/Child/\_search?routing=alice](http://localhost:9200/myindex/Child/_search?routing=alice)" -d '  
> > {  
> > "query" : {  
> > "term" : { "\_parent" : "alice" }  
> > }
> > 
> > }
> > 
> > Can someone please help me to get the first query working?
> > 
> > Thanks,  
> > Rukshan
> > 
> > --  
> > You received this message because you are subscribed to the Google Groups  
> > "elasticsearch" group.  
> > To unsubscribe from this group and stop receiving emails from it, send an  
> > email to [elasticsearc...@googlegroups.com](mailto:elasticsearc...@googlegroups.com) \<javascript:\>.  
> > To view this discussion on the web visit  
> > [https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com)[https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/5619eff7-b267-429b-afc3-e4c41aedc76b%40googlegroups.com?utm_medium=email&utm_source=footer)  
> > .  
> > For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/f2f6864b-b49f-4707-98af-8f7b58a01782%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/f2f6864b-b49f-4707-98af-8f7b58a01782%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 1:43am UTC](https://discuss.elastic.co/t/term-query-to-get-children-based-on-parent-id/16296/4 "2017-07-06T01:43:44Z")

</div>


