# Search querystring for combining parent and child docs

**URL:** https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375
**Category:** Elasticsearch
**Created:** [October 16, 2012, 10:25am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375 "2012-10-16T10:25:18Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![Ganesan\_Marappan](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Ganesan\_Marappan](https://discuss.elastic.co/u/Ganesan_Marappan)
#### Post date: [October 16, 2012, 10:25am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375/1 "2012-10-16T10:25:18Z")

</div>

Hi Friends

I'm new in Elastic Search. i'm developing [asp.net](http://asp.net)(c#) search application using ES.

i have used two tables. First one is Parent table and another one is Child Table. i can able to retrieve parent records using HasChildQuery. what my question is , i want to combine the parent and child table queries in single search query.

Ex:

Parent Table : [parent\_type]

## PC1 | PC2 | PC3 | PC4

R1 A B C  
R2 AA BB CC  
R3 AAA BBB CCC

Child Table : [child\_type]

## PC1 | CC1 | CC2 | CC3

R1 X Y Z  
R1 XX YY ZZ  
R2 XXX YYY ZZZ

I need the query string like this.

( PC1:"R1" AND [child\_type].CC1:"XX" )

Please provide the querystring for the above.

Note: Earlier i have used [Lucene.Net](http://Lucene.Net). Now i converted into Elastic Search and using the Lucene parcer.

---

<div class="post-metadata">

### Author: ![mvg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mvg/32/98890_2.png) [@mvg](https://discuss.elastic.co/u/mvg)
#### Post date: [October 16, 2012, 10:53am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375/2 "2012-10-16T10:53:22Z")

</div>

You can nest your HasChildQuery inside a bool query and also add any  
parent clauses to the bool query:

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

Something like this should work:  
"query" : {  
"bool" : {  
"must" : [  
{ "term" : { "PC1" : "R1" } },  
{ "has\_child" : {"type" : "child\_type", "query" : {"term"  
: {"CC1" : "XX"}}}}  
]  
}  
}

The above query should return R1.

Martijn

On 16 October 2012 12:25, ganesanmarappan [ganesmca@gmail.com](mailto:ganesmca@gmail.com) wrote:

> Hi Friends
> 
> I'm new in Elastic Search. i'm developing [asp.net](http://asp.net)(c#) search application  
> using ES.
> 
> i have used two tables. First one is Parent table and another one is Child  
> Table. i can able to retrieve parent records using HasChildQuery. what my  
> question is , i want to combine the parent and child table queries in single  
> search query.
> 
> Ex:
> 
> Parent Table : [parent\_type]
> 
> ## PC1 | PC2 | PC3 | PC4
> 
> R1 A B C  
> R2 AA BB CC  
> R3 AAA BBB CCC
> 
> Child Table : [child\_type]
> 
> ## PC1 | CC1 | CC2 | CC3
> 
> R1 X Y Z  
> R1 XX YY ZZ  
> R2 XXX YYY ZZZ
> 
> I need the query string like this.
> 
> ( PC1:"R1" AND [child\_type].CC1:"XX" )
> 
> Please provide the querystring for the above.
> 
> Note: Earlier i have used [Lucene.Net](http://Lucene.Net). Now i converted into Elastic Search  
> and using the Lucene parcer.
> 
> --  
> View this message in context: [http://elasticsearch-users.115913.n3.nabble.com/search-querystring-for-combining-parent-and-child-docs-tp4024046.html](http://elasticsearch-users.115913.n3.nabble.com/search-querystring-for-combining-parent-and-child-docs-tp4024046.html)  
> Sent from the Elasticsearch Users mailing list archive at [Nabble.com](http://Nabble.com).
> 
> --

--  
Met vriendelijke groet,

Martijn van Groningen

--

---

<div class="post-metadata">

### Author: ![Ganesan\_Marappan](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Ganesan\_Marappan](https://discuss.elastic.co/u/Ganesan_Marappan)
#### Post date: [October 16, 2012, 11:25am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375/3 "2012-10-16T11:25:58Z")

</div>

Thanks Martijn. The given seneario we have implemented and working fine for simple queries. if the query is complicated like below..

( PC1:"R1" AND [child\_type].CC1:"XX" ) OR (( PC1:"R2" OR [child\_type].CC1:"XXX" ) AND PC4:"CCC")

we are unable to build the query using HasChild.

Please let us know if query can be framed like [child\_type].CC1:"XX" in querystring search.

---

<div class="post-metadata">

### Author: ![Ganesan\_Marappan](https://avatars.discourse-cdn.com/v4/letter/g/f1d935/32.png) [@Ganesan\_Marappan](https://discuss.elastic.co/u/Ganesan_Marappan)
#### Post date: [October 17, 2012, 5:05am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375/4 "2012-10-17T05:05:04Z")

</div>

Thanks Martijn. The given seneario we have implemented and working fine for simple queries. if the query is complicated like below..

( PC1:"R1" AND [child\_type].CC1:"XX" ) OR (( PC1:"R2" OR [child\_type].CC1:"XXX" ) AND PC4:"CCC")

we are unable to build the query using HasChild.

Please let us know if query can be framed like [child\_type].CC1:"XX" in querystring search.

---

<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: [October 19, 2012, 10:41am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375/5 "2012-10-19T10:41:16Z")

</div>

On Tue, 2012-10-16 at 04:25 -0700, Ganesan Marappan wrote:

> Thanks Martijn. The given seneario we have implemented and working fine for  
> simple queries. if the query is complicated like below..
> 
> ( PC1:"R1" AND [child\_type].CC1:"XX" ) OR (( PC1:"R2" OR  
> [child\_type].CC1:"XXX" ) AND PC4:"CCC")
> 
> we are unable to build the query using HasChild.
> 
> Please let us know if query can be framed like [child\_type].CC1:"XX" in  
> querystring search.

Don't try to express your entire query in the query\_string - it is  
complicated, difficult to debug, and prone to error.

The Query DSL is your friend.

clint

--

---

<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, 3:07am UTC](https://discuss.elastic.co/t/search-querystring-for-combining-parent-and-child-docs/9375/6 "2017-07-06T03:07:55Z")

</div>


