为什么Hibernate Search索引无法正常工作

问题描述 投票:0回答:1

我已将Hibernate Search集成到我的后端以进行全文搜索,

该应用程序运行正常,我向数据库中添加了一些信息(请参见屏幕截图),但是有时当我搜索一个词(word =“ 1”)时,我只会得到一个结果,而不是多个结果(对于另一个词“例如:maroc”)我什么也没得到。我认为这是索引问题,而不是查询问题。

enter image description here

enter image description here

enter image description here

这是我的实体代码:

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.hibernate.search.annotations.*;

import javax.persistence.*;;
import java.util.Set;


@Entity
@Indexed
@Table(name="client")
public class Client {

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name="fullname")
    @Field(termVector = TermVector.YES,analyzer = @Analyzer(impl = StandardAnalyzer.class))
    String fullName;

    @Column(name="adress")
    @Field
    String adress;

这是我的SearchService代码:

import org.apache.lucene.search.Query;
import org.hibernate.search.jpa.FullTextEntityManager;
import org.hibernate.search.jpa.Search;
import org.hibernate.search.query.dsl.QueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import java.util.List;

@Component
public class SearchService {


    @Autowired
    private EntityManager entityManager;


    @Autowired
    public SearchService( EntityManagerFactory entityManagerFactory) {
        super();
        this.entityManager = entityManagerFactory.createEntityManager();
    }

    public void initializeHibernateSearch() {
        try {
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
            fullTextEntityManager.createIndexer().startAndWait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Transactional
    public List<Client> clientSearch(String searchTerm) {

        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
        QueryBuilder qb = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(Client.class).get();
        Query luceneQuery = qb.keyword().fuzzy().withEditDistanceUpTo(1).withPrefixLength(1).onFields("id","adress","fullName")
                .matching(searchTerm).createQuery();

        javax.persistence.Query jpaQuery = fullTextEntityManager.createFullTextQuery(luceneQuery, Client.class);

        // execute search

        List<Client> clientList = null;
        try {
            clientList = jpaQuery.getResultList();
        } catch (NoResultException nre) {
        }

        return clientList;
    }

这是我的HibernateSearchConfiguration代码:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.persistence.EntityManagerFactory;

@Configuration
@EnableAutoConfiguration

public class HibernateSearchConfiguration {

    private EntityManagerFactory entityManagerFactory;

    @Autowired
    public HibernateSearchConfiguration(EntityManagerFactory entityManagerFactory) {
        this.entityManagerFactory = entityManagerFactory;
    }

    @Bean
    SearchService hibernateSearchService() {
        SearchService hibernateSearchService = new SearchService(this.entityManagerFactory);
        hibernateSearchService.initializeHibernateSearch();
        return hibernateSearchService;
    }
}
spring indexing lucene full-text-search hibernate-search
1个回答
0
投票

您确实应该在所有文本字段中使用相同的分析器。如果您未指定分析仪,则将使用默认的分析仪,这可能会导致您遇到问题。

    @Id
    @Column(name="id")
    @GeneratedValue(strategy=GenerationType.AUTO)
    private long id;

    @Column(name="fullname")
    @Field(termVector = TermVector.YES,analyzer = @Analyzer(impl = StandardAnalyzer.class))
    String fullName;

    @Column(name="adress")
    @Field(analyzer = @Analyzer(impl = StandardAnalyzer.class))
    String adress;
© www.soinside.com 2019 - 2024. All rights reserved.