How to handle one-to-many relations?


(Alex Wajda) #1

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.


(talsalmona) #2

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 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.


(system) #3