# Many to one parent child relationship

**URL:** <https://discuss.elastic.co/t/many-to-one-parent-child-relationship/101595>\
**Category:** Elasticsearch\
**Created:** [September 24, 2017, 7:38am UTC](https://discuss.elastic.co/t/many-to-one-parent-child-relationship/101595 "2017-09-24T07:38:22Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![kumar\_soumitra](https://avatars.discourse-cdn.com/v4/letter/k/a6a055/32.png) [@kumar\_soumitra](https://discuss.elastic.co/u/kumar_soumitra)\
**Post date:** [September 24, 2017, 7:38am UTC](https://discuss.elastic.co/t/many-to-one-parent-child-relationship/101595/1 "2017-09-24T07:38:22Z")

</div>

I have an index which stores metrics of applications during its lifecycle. There are 0 or more documents when application '1' is running and 1 document when application has finished. I am using parent-child relationship to store tags of each application.

As per [https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html](https://www.elastic.co/guide/en/elasticsearch/guide/current/parent-child.html) , parent-child is one-to-many relationship. But, is there a way to make many-to-one relationship work using routing, and parent fields?

Here is my attempt to make it work, but it does not, is there a way? I am using elasticsearch-5.5.2 .

Lets say there are N numbers of documents for application id '1', i.e. N-1 documents in state RUNNING, and 1 in state FINISHED, and there are M number of tags associated with this application (appid '1'). I wish to index N documents in type 'apps' and M documents in type 'tags' and use has\_child query to filter RUNNING and and FINISHED applications.

Here is my template definition

$ cat template.app.json  
{  
"template": "apps-\*",  
"mappings": {  
"_default_": {  
"dynamic" : "strict",  
"date\_detection": false,  
"\_routing": { "required": true },  
"\_all": { "enabled": false },  
"\_source": { "enabled": true },  
"properties": {  
"appid": { "type" : "string", "index" : "not\_analyzed"},  
"state": { "type" : "string", "index" : "not\_analyzed"},  
"progress": { "type": "float", "index": "no"},  
"memory": { "type": "long", "index": "no"},  
"date": { "type": "date"},  
"key": { "index": "not\_analyzed", "type": "string" },  
"value": { "index": "not\_analyzed", "type": "string" },  
"comment": { "type": "string", "store": false }  
}  
},  
"apps": {},  
"tags": {  
"\_parent": { "type" : "apps" }  
}  
}  
}

I am using \_routing, and \_parent field during indexing to make sure that all tags and apps documents for a particular application are indexed in the same shard.

PUT app\_index/apps/1?routing=1  
PUT app\_index/apps/1-r1?routing=1  
PUT app\_index/apps/1-r2?routing=1

PUT app\_index/tags/1t1?routing=1&parent=1  
PUT app\_index/tags/1t2?routing=1&parent=1

1. Get all the FINISHED applications which have "user" tag. This works as expected.  
GET /\_search  
{  
"query": {  
"bool": {  
"must": [  
{ "term": { "state": "FINISHED" } },  
{ "has\_child" : { "type": "tags", "query": { "bool": { "must": [{ "term": { "key": "user" } }] } } } }  
]  
}  
}  
}

2. Get all the RUNNING applications which have "user" tag. This does not work, since the parent id of tags are pointing to FINISHED apps.  
GET /\_search  
{  
"query": {  
"bool": {  
"must": [  
{ "term": { "state": "RUNNING" } },  
{ "has\_child" : { "type": "tags", "query": { "bool": { "must": [{ "term": { "key": "user" } }] } } } }  
]  
}  
}  
}

I don't want to duplicate tags for every RUNNING documents, is there a way to use has\_child query, or any other way to query (scripted, or pipeline) to make it work? I would greatly appreciate any help here.

---

<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:** [October 22, 2017, 7:39am UTC](https://discuss.elastic.co/t/many-to-one-parent-child-relationship/101595/2 "2017-10-22T07:39:01Z")

</div>

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