I'm using spring boot v3.1.2. I'm trying to implement the search feature using Hibernate search with Lucene. For the configuration, I followed the following documentation
I'm struggling bcz manything have been changed in the latest versions of Hibernate Search ! the searchByQuery returns an empty list even though I expect data to match the query
@RequiredArgsConstructor
public class ProductRepositoryImpl implements ProductRepository {
private final EntityManager entityManager;
@Override
public List<Product> searchByQuery(String query, Pageable pageable) throws RuntimeException {
SearchSession searchSession = Search.session( entityManager );
SearchResult<Product> result = searchSession.search( Product.class )
.where( f -> f.match()
.fields( "title", "description" )
.matching( query ) )
.fetch( 20 );
long totalHitCount = result.total().hitCount();
List<Product> hits = result.hits();
return hits;
}
}
I suspect that the data hasn't been indexed yet ! any insights on what I might have overlooked ?
// Product.java
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField;
import org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed;
@Table(name = "products")
@Getter
@Setter
@Builder
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Indexed
public class Product extends BaseEntity {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
@FullTextField
private String title;
private String code;
@FullTextField
private String description;
...
Update
Still doesn't able to index correctly the data, I've created a basic example here https://github.com/smaillns/demo-hibernate-search
Get /book/search?query=prod should return items !
To index the pre-existing data on application bootstrap, we can add this bean
we should then get something like that in the logs
Note