I have a parent-child relationship.
My parent documents have a field called lastRevisionId
which stores an identifier for the most recent revision of the parent document.
The child documents are immutable key/value pairs describing properties of a specific version of the parent.
Parent
{
"_id": "parent-1",
"lastRevisionId": "rev1"
}
Child
{
"_id": "parent-1|rev1|name",
"revisionId": "rev1",
"key": "name",
"value": "Test Server"
}
For each revision, we'd index a new child document for the new revision, so after we update Parent to rev2, we'd have this in ES:
Parent
{
"_id": "parent-1",
"lastRevisionId": "rev2"
}
Children
{
"_id": "parent-1|rev1|name",
"revisionId": "rev1",
"key": "name",
"value": "Test Server"
}
{
"_id": "parent-1|rev2|name",
"revisionId": "rev2",
"key": "name",
"value": "Updated Server"
}
What I need is a query that will return Child documents where the revisionId
on a the child document equals the lastRevisionId
on the Parent.
The reasoning for this is, we want to store historical data of these revisions, but we also want to be able to only fetch the latest information. The historical data is only really useful from an auditing perspective, but the latest would describe a large set of useful current-state.
We are trying to avoid denormalizing this data, as any single revision of the parent could have hundreds to thousands of child documents, and updating them all for every change would introduce its own scaling problems.
If this were SQL, the query would look something like:
SELECT c.* FROM child as c
INNER JOIN parent as p
ON p.lastRevisionId= c.revisionId