如何在 Spring Boot 中使用 JPA + MySQL 实现全文搜索

Kon*_*kos 6 full-text-search hibernate hibernate-search spring-data-jpa spring-boot

public class Product {

    private Long id;    

    private String name;

    private String description;

}
Run Code Online (Sandbox Code Playgroud)

有没有办法用 JpaRepository 实现对 Product 类的描述的全文搜索?

yro*_*ere 13

您可以使用Hibernate Search,这是一个插入 Hibernate ORM 的库,当您将实体发送到数据库时,它可以在文件系统上动态地将您的实体索引到 Lucene 索引中。请参阅入门指南

编辑:现在 Hibernate Search 也支持使用 Elasticsearch 作为后端;它会自动在 Elasticsearch 或本地 Lucene 索引中复制您的数据库的一部分,无论您选择哪个。)

查询 Lucene/Elasticsearch 索引与查询数据库有点不同,因此您不能像通常那样使用 HQL 或 Criteria。Hibernate Search 提供了它自己的查询 DSL。

如果您想在您的存储库中使用自动神奇生成的方法实现,您可以依赖Snowdrop,这是一个 Hibernate Search / Spring Data 集成,但它已经有一段时间没有更新了。

最好的办法可能是在存储库接口中定义查询方法,然后使用 Hibernate Search API 自己实现它们。它真的没有那么复杂,通常建议用于除最明显的查询之外的所有查询。请参阅Spring Data JPA 文档

本质上,您将拥有类似于以下代码段的内容。请记住,在此操作之前,您需要重新索引数据库!有关更多信息,请参阅入门指南

使用休眠搜索 6+:

@Indexed // Add this
public class Product {

    private Long id;    

    @FullTextField // And this
    private String name;

    @FullTextField // And this
    private String description;

}

public interface ProductRepository extends CrudRepository<Product, Long>, CustomizedProductRepository {
  // Declare automatically generated methods here
}


public interface CustomizedProductRepository {
  List<Product> search(String terms, int limit, int offset);
}



public class CustomizedProductRepositoryImpl implements CustomizedProductRepository {

  @PersistenceContext
  private EntityManager em;

  @Override
  public List<Product> search(String terms, int limit, int offset) {
    return Search.session(em).search(Product.class)
            .where(f -> f.match()
                    .fields("name", "description")
                    .matching(terms))
            .fetchHits(offset, limit);
  }
}
Run Code Online (Sandbox Code Playgroud)

或使用休眠搜索 5:

@Indexed // Add this
public class Product {

    private Long id;    

    @Field // And this
    private String name;

    @Field // And this
    private String description;

}

public interface ProductRepository extends CrudRepository<Product, Long>, CustomizedProductRepository {
  // Declare automatically generated methods here
}


public interface CustomizedProductRepository {
  List<Product> search(String terms, int limit, int offset);
}



public class CustomizedProductRepositoryImpl implements CustomizedProductRepository {

  @PersistenceContext
  private EntityManager em;

  @Override
  public List<Product> search(String terms, int limit, int offset) {
    FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);

    QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory()
        .buildQueryBuilder().forEntity(Product.class).get();
    org.apache.lucene.search.Query luceneQuery = queryBuilder
        .keyword()
        .onFields("name", "description")
        .matching(terms)
        .createQuery();

    // wrap Lucene query in a javax.persistence.Query
    javax.persistence.Query jpaQuery =
        fullTextEntityManager.createFullTextQuery(luceneQuery, Product.class);

    jpaQuery.setMaxResults(limit);
    jpaQuery.setFirstResult(offset);

    // execute search
    return jpaQuery.getResultList();
  }
}
Run Code Online (Sandbox Code Playgroud)