Quote:
I want to find all X instances that contain a Y with specific id and name. Searching by X.Y.id and X.Y.name is not enough because it won't guarantee that id and name matched properties on the same Y instance.
You are correct, you won't have that guarantee.
Quote:
Is it possible to do this?
Not really. You have to bear in mind that Lucene is not relational at all: we can de-normalize some kind of relations (mostly ManyToOne and OneToOne), but your case is the typical example of what is not supported by the engine without some tricks from your part.
One option would be to encode id+name in the same field. This is easy if "name" is just a keyword for which you don't want to apply an analyzer (you can't let an Analyzer blindly process your tricked value or it will split it and it won't work). If you need to analyze the name, you'll have to pre-tokenize it and then store in the field as id+token1, id+token2, ..., id+tokenN. Remember: you can index the same field multiple times.
To query such an encoded field, would need some similar token manipulation: not trivial but doable.
The good news is that Lucene is coming with a new (limited) JOIN functionality; what you need to do might become simpler in the future but the upcoming feature is going to have a significant performance hit: if you can apply the above trick that should be best.