I followed the getting started guide for Spring Boot with Hibernate Search 6.1.6 and Lucene.
I get duplicates when using a simple search query like this:
Search.session(entityManager).search(User.class)
.where(f -> f.match().fields("name", "email").matching("john").fuzzy()).fetchAllHits();
My User class is configured like described in the documentation:
@Table(name = "user)
@Entity
@Indexed
public class User {
@Id
private Long id;
@FullTextField
private String name;
@FullTextField
private String email;
}
What could be the cause for getting duplicates?
You basically forgot to purge/drop your indexes at some point.
Most likely you’ve run the mass indexer with .purgeAllOnStart(false)
. This will add new entries to the index without removing existing ones, resulting in duplicates, unless you know for sure that your index is already empty.
Another way to end up in this situation is if you’ve started playing with your application, initializing your database through manual calls to the entity manager (.persist()
, …) which also initialized your index, and then you dropped your database but not your index and started from the beginning, initializing your database through manual calls to the entity manager again, which added new entries to the index without removing the existing ones (why should it, since the entities are supposed to be new?)