I'm trying to model one-to-many relationships in Elasticsearch with ability to fetch parents and children without involving any searches.
Question 1: I thought that the way I can achieve this is by using _id
wherever possible. Can someone confirm if fetching a document by doing GET
request to /index/type/1
faster than doing a term query on a field that's not _id
?
Below is an example of 1 parent and its 2 children.
Parent (type: parent):-
{
"_id": "deterministic_id_1",
"app": "awesome-app-1",
"server": "awesome-server-1"
}
Children (type: children):-
{
"config": "awesome-config-1"
"_parent": "deterministic_id_1"
}
{
"config": "awesome-config-2"
"_parent": "deterministic_id_1"
}
I have deterministic _id field so that I can fetch parent documents without searching. That already works, simply do a GET
request for /index/type/<id>
.
I know that Elasticsearch maintains a mapping of all parent->child relationships internally. Now, how do I fetch all children for a parent without searching? Is there a way? Best I could come up with is:-
POST /index/children/_search
{
"query": {
"has_parent": {
"type": "parent",
"query": {
"ids": {
"type": "config_parent",
"_id": "deterministic_id_1"
}
}
}
}
}
Question 2: Does Is this the fastest way I can fetch all children for the parent with _id
as deterministic_id_1
? Does this query hit all shards?
I want two things from my data:-
- Get access to all parents using my deterministic_id_1. (already done)
- Get access to all children using the parent's document id without involving any searching. (pending)