NPE while creating org.apache.lucene.search.Query

无人久伴 提交于 2020-01-06 05:43:47

问题


I wanted to make Full text Search via my Spring Boot application with the help of Hibernate Search. But I faced with such a problem: NPE while creating org.apache.lucene.search.Query.

Here is my code

@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;
    }
} 

It occurs on this line

org.apache.lucene.search.Query luceneQuery = qb
            .keyword()
            .onFields("name")
            .matching(keyword)
            .createQuery();

My entity:

@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;
}

My properties:

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 dependencies:

<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>

EDIT

Solution found The cause was that I pass null string into fulltext search method

来源:https://stackoverflow.com/questions/45328071/npe-while-creating-org-apache-lucene-search-query

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!