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

2019-09-16 15:18发布

问题:

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