# Query both parent/child documents

**URL:** <https://discuss.elastic.co/t/query-both-parent-child-documents/71771>\
**Category:** Elasticsearch\
**Created:** [January 16, 2017, 5:51pm UTC](https://discuss.elastic.co/t/query-both-parent-child-documents/71771 "2017-01-16T17:51:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![enp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/enp/32/3825_2.png) [@enp](https://discuss.elastic.co/u/enp)\
**Post date:** [January 16, 2017, 5:51pm UTC](https://discuss.elastic.co/t/query-both-parent-child-documents/71771/1 "2017-01-16T17:51:52Z")

</div>

Hi,

I have some documents with parent-child relationship:

```
curl -XPUT 'http://localhost:9200/data?pretty=1' -d '
{
   "mappings" : {
      "destination" : {},
      "venue" : {
         "_parent" : {
            "type" : "destination"
         }
      }
   }
}
'

curl -XPOST 'http://localhost:9200/wg_data/_bulk?pretty=1' -d '
{"index":{"_id":"472","_type":"destination"}}
{"city_id":"49","country_code":"DE","id":"472","city_name":"Munich"}
{"index":{"_parent":"472","_id":"2804","_type":"venue"}}
{"subcategories":["Falafel Restaurant","Middle Eastern Restaurant"],"name":"Sababa","categories":["Restaurant"],"id":"2804"}
{"index":{"_parent":"472","_id":"2805","_type":"venue"}}
{"subcategories":["Cafe","Cocktail Bar","Italian Restaurant"],"name":"Bar Centrale","categories":["Bar","Restaurant"],"id":"2805"}
{"index":{"_parent":"472","_id":"2806","_type":"venue"}}
{"subcategories":["Restaurant","Bar"],"name":"LUX Restaurant & Bar","categories":["Bar"],"id":"2806"}
{"city_id":"40","country_code":"FR","id":"465","city_name":"Paris"}
{"index":{"_parent":"465","_id":"1868","_type":"venue"}}
{"subcategories":["French Restaurant"],"name":"Benedict","categories":["Restaurant"],"id":"1868"}
{"index":{"_parent":"465","_id":"1870","_type":"venue"}}
{"subcategories":["Restaurant","Gourmet Shop","Bar"],"name":"Comptoir Gourmet","categories":["Bar","Shop"],"id":"1870"}
{"index":{"_parent":"465","_id":"1871","_type":"venue"}}
{"subcategories":["French Restaurant"],"name":"Pamela Popo","categories":["Restaurant"],"id":"1871"}
{"index":{"_parent":"465","_id":"1872","_type":"venue"}}
{"subcategories":["French Restaurant"],"name":"Chez Julien","categories":["Restaurant"],"id":"1872"}
{"index":{"_parent":"465","_id":"8426","_type":"venue"}}
{"subcategories":["Jazz Club"],"name":"Sunset","categories":["Activity"],"id":"8426"}

```

Is it possible to select paren/child pair by parent country\_code (FR) and child subcategory ("Jazz Club") by one query? So, I want to see something like this as result:

```
{"city_id":"40","country_code":"FR","id":"465","city_name":"Paris", venues:
[{"subcategories":["Jazz Club"],"name":"Sunset","categories":["Activity"],"id":"8426"}]
}

```

I see only one way now - find parent id first by country\_code:

```
GET data/destination/_search 
{
  "query": { "term": {
    "country_code.keyword": {
      "value": "FR"
    }
  }}
}

```

and child by parent id and subcategory:

```
GET data/venue/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "parent_id": {
            "type": "venue",
            "id": "465"
          }
        },
        {
          "term": {
            "subcategories.keyword": {
              "value": "Jazz Club"
            }
          }
        }
      ]
    }
  }
}

```

Maybe more simple way exists?

---

<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:** [February 13, 2017, 5:52pm UTC](https://discuss.elastic.co/t/query-both-parent-child-documents/71771/2 "2017-02-13T17:52:00Z")

</div>

Closed on February 13, 2017, 5:52pm UTC

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