# How to handle one-to-many relations?

**URL:** <https://discuss.elastic.co/t/how-to-handle-one-to-many-relations/3223>\
**Category:** Elasticsearch\
**Created:** [August 16, 2010, 12:00pm UTC](https://discuss.elastic.co/t/how-to-handle-one-to-many-relations/3223 "2010-08-16T12:00:20Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Alex\_Wajda](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/alex_wajda/32/3319_2.png) [@Alex\_Wajda](https://discuss.elastic.co/u/Alex_Wajda)\
**Post date:** [August 16, 2010, 12:00pm UTC](https://discuss.elastic.co/t/how-to-handle-one-to-many-relations/3223/1 "2010-08-16T12:00:20Z")

</div>

Hi folks,

I have the following domain model:  
[Office] -----\* [Employee]

Employee has a reference to the corresponding Office object.  
I want to index them independently and also want to be able to search  
for employees by the office name, e.q. "\_search?  
q=employee.office.name:Head"  
So I toss the following JSONs to ElasticSearch:

Office:  
{  
id: 1,  
name: "Head office"  
}

and

Employee:  
{  
id: 123,  
name: "Alex Wajda",  
office: {  
id: 1,  
name: "Head Office"  
}  
}

But when I change the office name I push the changed Office JSON to  
the index and I need somehow to tell ElasticSearch to reindex all the  
corresponding employee documents without having to do it explicitly.  
So in my example I execute:

curl -XPUT localhost:9200/myindex/office/1 -d '  
{  
id: 1,  
name: "Headquarter"  
}'

And I expect the following query to return Alex Wajda's Employee  
object with the updated office properties:  
curl -XPUT /myindex/employee/\_search?  
q=employee.office.name:Headquarter

Is there any way how to achieve this goal?

Thanks,  
BR, Alex.

---

<div class="post-metadata">

**Author:** ![talsalmona](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/talsalmona/32/3316_2.png) [@talsalmona](https://discuss.elastic.co/u/talsalmona)\
**Post date:** [August 16, 2010, 4:56pm UTC](https://discuss.elastic.co/t/how-to-handle-one-to-many-relations/3223/2 "2010-08-16T16:56:44Z")

</div>

No, there is no notion of relations in Elasticsearch.  
You will have to programatically reindex the Employee document.  
The best way to do this is to search for all employee documents with  
the old office name, parse the \_source field (that holds the original  
JSON), make the appropriate changes in the JSON and reindex.

Tal

On Aug 16, 3:00 pm, Alex Wajda [alexander.wa...@gmail.com](mailto:alexander.wa...@gmail.com) wrote:

> Hi folks,
> 
> I have the following domain model:  
> [Office] -----\* [Employee]
> 
> Employee has a reference to the corresponding Office object.  
> I want to index them independently and also want to be able to search  
> for employees by the office name, e.q. "\_search?  
> q=employee.office.name:Head"  
> So I toss the following JSONs to Elasticsearch:
> 
> Office:  
> {  
> id: 1,  
> name: "Head office"
> 
> }
> 
> and
> 
> Employee:  
> {  
> id: 123,  
> name: "Alex Wajda",  
> office: {  
> id: 1,  
> name: "Head Office"  
> }
> 
> }
> 
> But when I change the office name I push the changed Office JSON to  
> the index and I need somehow to tell Elasticsearch to reindex all the  
> corresponding employee documents without having to do it explicitly.  
> So in my example I execute:
> 
> curl -XPUT localhost:9200/myindex/office/1 -d '  
> {  
> id: 1,  
> name: "Headquarter"
> 
> }'
> 
> And I expect the following query to return Alex Wajda's Employee  
> object with the updated office properties:  
> curl -XPUT /myindex/employee/\_search?  
> q=employee.office.name:Headquarter
> 
> Is there any way how to achieve this goal?
> 
> Thanks,  
> BR, Alex.

---

<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, 4:20am UTC](https://discuss.elastic.co/t/how-to-handle-one-to-many-relations/3223/3 "2017-07-06T04:20:41Z")

</div>

Closed on July 6, 2017, 4:20am UTC


