0

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
Matthew Shea
  • 567
  • 5
  • 14

1 Answers1

0

you can use Nested Mapping for writing your query : elastic.co/guide/en/elasticsearch/reference/current/nested.html

nitashathakur
  • 430
  • 3
  • 9
  • I fear I may not have communicated what I'm up to. It looks like `nested` is for situations where I have an array of objects within a document that you'd want to query. In my case, I have three distinct documents. – Matthew Shea Jan 24 '20 at 06:04
  • try using a flag for lastRevisionId, as proposed here : https://stackoverflow.com/questions/8218309/can-we-retrieve-previous-source-docs-with-elastic-search-versions/8226684#8226684 – nitashathakur Jan 28 '20 at 02:47