Multiple nested collections search

Hi, I have a problem when trying to search for a specific value in my entity’s subclasses as there is multiple nested collections structure.
I got situation like this:

Superclass{
private AnObject anObject;
...
//other fields to query
}

AnObject{
private Map<EnumKey, AnotherObject> anotherObjects;
...
}

AnotherObject{
private List<ThirdObject> thirdObjects;
...
}

ThirdObject{
private Object fieldToQuery;
...
}

What I need to do is to check if in Superclass.anObject map item from anotherObjects with specific key (for this example lets say that key value is 1L) contains in any of list thirdObject a specific value in fieldToQuery

ForExample: i want to get item from map with key 1L and fieldToQuery value of “x” and only entity in my database is like this:

Superclass : {
     anObject : {
          anotherObjects : {
               1L -> {
                     thirdObjects: (doesn't contain any Object with fieldtoQuery value "x")
                }
               2L -> {
                     thirdObjects: (contains an Object with fieldtoQuery value "x")
               }
          }
     }
}

I should get empty SearchResult as there is no value “x” in element under key 1L.

Is it possible and please explain how to do it?

Hey Filip,

Your question looks similar to the one discussed in this thread Searching in nested lists

Overall you want to use a nested structure on your index-embedded and a nested predicate. In particular take a look at the example from that section (it shows how to build a predicate where two fields of a nested object must match the condition for an entity to be included in the results.

The problematic part is that you need to have the map key be a part of the object representing the corresponding value. (using a value extractor for both keys and values won’t work since you’ll just end up having two collections)

So you could try to have something along the next lines instead:

AnObject{
    // don't map the map itself
    private Map<EnumKey, AnotherObject> anotherObjects;

    // have a getter instead that would return a custom object:
    @IndexedEmbedded(structure = ObjectStructure.NESTED)
    @IndexingDependency(derivedFrom = {
		@ObjectPath(@PropertyValue(propertyName = "anotherObjects"))})
    List<AnotherObjectsWrapped> getAnotherObjectsWrapped() {
    	return anotherObjects.entrySet().stream().map( AnotherObjectsWrapped::new ).collect( Collectors.toList() );
    }

}

public static class AnotherObjectsWrapped {
	@GenericField
	EnumKey enumKey;
	@IndexedEmbedded
	AnotherObject anotherObject;
	public BookEntry(Map.Entry<EnumKey,AnotherObject> entry) {
		...
	}
}

Also relevant: [HSEARCH-3689] - Hibernate JIRA (not implemented yet)