Hi,
Do we have any solution for problem specified here .
This is the same case happens in HS result as well.
I know we can use Set instead of List (beg) to prevent this issue.
However code complexity not allowing me to do so.
If any work around for this in HS please let me know
Thanks in advance
Hibernate Search does not have a built-in method of returning hierarchical results.
I’d recommend this approach. It’s slower than alternatives (because you execute two queries), but it’s rather simple and flexible.
- Index both
Parent
andChild
, with@IndexedEmbedded
on the propertyParent.children
, and@IndexedEmbedded(includePaths = "id")
on the propertyChild.parent
. You’ll need to put@GenericField
onParent.id
, of course. - When querying, query the
Parent
first; if you need to queryChild
fields, usechild.someField
instead ofsomeField
. You may need to rely on thenested
predicate (HSearch 6 only) in some cases. - Then, collect the IDs of all matched
Parent
s. - Then execute a search query again, this time on
Child
, with an additional predicate to require that the fieldparent.id
matches any of the collected IDs. - Then build a
Map<Parent, List<Child>>
from the search results.
EDIT: Alternatively, if you don’t mind a single Parent
being spread on two pages of results, you can simply search on the children, and sort by parent ID. Then building the map for each page should be trivial.