HS 6 prevent duplicates in result

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 and Child, with @IndexedEmbedded on the property Parent.children, and @IndexedEmbedded(includePaths = "id") on the property Child.parent. You’ll need to put @GenericField on Parent.id, of course.
  • When querying, query the Parent first; if you need to query Child fields, use child.someField instead of someField. You may need to rely on the nested predicate (HSearch 6 only) in some cases.
  • Then, collect the IDs of all matched Parents.
  • Then execute a search query again, this time on Child, with an additional predicate to require that the field parent.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.