我想在Hibernate Search的帮助下通过我的Spring Boot应用程序进行全文搜索。但我遇到了这样一个问题:创建org.apache.lucene.search.Query
时的NPE。
这是我的代码
@Repository
public class HibernateSearch {
@PersistenceContext
private EntityManager entityManager;
@Transactional
public List<Manual> fulltextSearching(String keyword){
FullTextEntityManager fullTextEntityManager =
org.hibernate.search.jpa.Search.getFullTextEntityManager(entityManager);
QueryBuilder qb = fullTextEntityManager.getSearchFactory()
.buildQueryBuilder().forEntity(Manual.class).get();
org.apache.lucene.search.Query luceneQuery = qb
.keyword()
.onFields("name")
.matching(keyword)
.createQuery();
javax.persistence.Query jpaQuery =
fullTextEntityManager.createFullTextQuery(luceneQuery, Manual.class);
@SuppressWarnings("unchecked")
List<Manual> result = jpaQuery.getResultList();
return result;
}
}
它出现在这一行
org.apache.lucene.search.Query luceneQuery = qb
.keyword()
.onFields("name")
.matching(keyword)
.createQuery();
我的实体:
@Entity
@Table(name = "manual")
@Getter
@Setter
@NoArgsConstructor
@Indexed
public class Manual {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "manual_id")
private Long id;
@NotNull
@Column(name = "manual_name")
@Field
private String name;
}
我的属性:
spring.jpa.properties.hibernate.search.default.directory_provider = filesystem
spring.jpa.properties.hibernate.search.default.indexBase = D:\\Java
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
My Maven依赖项:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-search-orm</artifactId>
<version>5.7.1.Final</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
</dependency>
修改
找到解决方案 原因是我将空字符串传递给全文搜索方法