Search parents by latest child

Here's the gist of my data
scheme: https://gist.github.com/rauanmaemirov/7b3af9106ccc2963d2a5

There are a collection of entities as parents and a collection of events as
child documents.
What I need to do is search documents by the latest event of a particular
type.

If you run that script on localhost (test index), you could see that search
request returns all 4 test entities, even though latest event for doc 2
doesn't match with the query terms.
I need a way to filter by the latest events and then apply query terms, is
it possible?

--
You received this message because you are subscribed to the Google Groups "elasticsearch" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elasticsearch+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/03263058-a180-45f2-8a2b-e13d4c9427a5%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.