Search code examples
lucenehibernate-search

Hibernate Search: predicate with multiple IDs from the IndexedEmbedded (similar to .id())


I am trying to find a way so that I can make the following use case work: Author is embedded in the Book (that is indexed), and I save the IDs of the Author in the book (author.id). I have a list of Author IDs that I would like to search for.

Is the best way to do this using .terms()? Or is there any better way? Thanks in advance!


Solution

  • Is the best way to do this using .terms()?

    Yes.

    List<Long> myIds = List.of( 1L, 2L );
    List<Book> hits = searchSession.search( Book.class )
            .where( f -> f.terms().field( "authors.id" )
                    .matchingAny( myIds ) )
            .fetchHits( 20 );
    

    Here's the documentation: https://docs.jboss.org/hibernate/stable/search/reference/en-US/html_single/#search-dsl-predicate-terms