I am trying to integrate sleep search in my project. My models are indexed, but for some reason my search queries do not return any results. I tried to solve this problem for several hours, but nothing works.
Domain Object:
@Entity @Table(name = "roles") @Indexed public class Role implements GrantedAuthority { private static final long serialVersionUID = 8227887773948216849L; @Id @GeneratedValue @DocumentId private Long ID; @Column(name = "authority", nullable = false) @Field(index = Index.TOKENIZED, store = Store.YES) private String authority; @ManyToMany @JoinTable(name = "user_roles", joinColumns = { @JoinColumn(name = "role_id") }, inverseJoinColumns = { @JoinColumn(name = "username") }) @ContainedIn private List<User> users; ... }
DAO:
public abstract class GenericPersistenceDao<T> implements IGenericDao<T> { @PersistenceContext private EntityManager entityManager; ... @Override public FullTextEntityManager getSearchManager() { return Search.getFullTextEntityManager(entityManager); } }
Services:
@Service(value = "roleService") public class RoleServiceImpl implements RoleService { @Autowired private RoleDao roleDAO; ... @Override @SuppressWarnings("unchecked") public List<Role> searchRoles(String keyword) throws ParseException { FullTextEntityManager manager = roleDAO.getSearchManager(); TermQuery tquery = new TermQuery(new Term("authority", keyword)); FullTextQuery query = manager.createFullTextQuery(tquery, Role.class); return query.getResultList(); } }
Test:
@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = { "classpath:applicationContext.xml" }) @Transactional public class TestRoleService extends Assert { @Autowired private RoleService roleService; @Test public void testSearchRoles() { roleService.saveRole(); List<Role> roles = roleService.searchRoles("test"); assertEquals(1, roles.size());
Configuration
<persistence-unit name="hibernatePersistence" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <properties> <property name="hibernate.search.default.directory_provider" value="org.hibernate.search.store.FSDirectoryProvider" /> <property name="hibernate.search.default.indexBase" value="indexes" /> </properties> </persistence-unit> <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean"> <property name="persistenceUnitName" value="hibernatePersistence" /> </bean> <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> <property name="entityManagerFactory" ref="entityManagerFactory" /> </bean> <tx:annotation-driven transaction-manager="transactionManager" /> <context:component-scan base-package="org.myproject" />
In fact, the database is filled with the role corresponding to this value of the authorization field. The object manager is valid, like all my regular CRUD tests. The meaning of the error is a completely sleeping search (3.1.1.GA), but where does this go wrong?
source share