【问题标题】:Dropwizard Hibernate SearchDropwizard 休眠搜索
【发布时间】:2018-02-16 16:24:35
【问题描述】:

我正忙于Dropwizard 应用程序,并想在我的数据库中搜索某些实体。我已经看到这可以通过在Entity 类的顶部创建NamedQuery 语句来完成,就像在this tutorial 中一样。然后从 DAO 类执行查询。

我偶然发现了Hibernate search,它似乎是在数据库中搜索实体时更好的解决方案。因此我的问题是 Dropwizard 框架是否支持它?此外,如果支持它,将如何配置和使用它?我在Dropwizard's hibernate documentation 中找不到任何对它的引用。

【问题讨论】:

  • 您具体需要什么支持?如果你的 DAO 扩展了 DW 的 AbstractDAO,你可以访问休眠会话并使用 Search.getFullTextSession(session) 获取搜索会话
  • 我想知道 Dropwizard 中是否包含 Hibernate Search 功能,即是否存在依赖关系,如果存在,应该如何配置和使用它?我似乎找不到有关如何完成此操作的教程或示例。

标签: java hibernate dropwizard


【解决方案1】:

通过反复试验,我设法弄清楚了如何在我的 Dropwizard 应用程序中使用休眠搜索。事实证明,Dropwizard 不直接支持它,但可以通过少量努力添加。我做了以下事情:

1.在我的pom文件中添加了Hibernate Search依赖:

    <!-- Search -->
    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-search-orm</artifactId>
        <version>5.9.0.Final</version>
    </dependency>

2.在我的应用程序的run-方法中,我创建了索引器:

           // search functionality
        try {
            EntityManager em = hibernate.getSessionFactory().createEntityManager();
            FullTextEntityManager  fullTextEntityManager = Search.getFullTextEntityManager(em);

            fullTextEntityManager.createIndexer().startAndWait();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

3.使用 @Indexed@Field 注释使我的实体类可搜索

@Entity
@Indexed
@Table(name = "product")
public class Product {

    @Id
    private int id;

    @Field(termVector = TermVector.YES)
    private String productName;

    @Field(termVector = TermVector.YES)
    private String description;

    @Field
    private int memory;

    // getters, setters, and constructors
} 

4.然后在我的 DAO 类中,我可以按如下方式搜索实体:

public List<Product> search(String term) {
        EntityManager em = this.currentSession();

        FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(em);

        // create native Lucene query unsing the query DSL
        // alternatively you can write the Lucene query using the Lucene query parser
        // or the Lucene programmatic API. The Hibernate Search DSL is recommended though
        QueryBuilder qb = fullTextEntityManager.getSearchFactory()
                .buildQueryBuilder()
                .forEntity(Product.class)
                .get();

        org.apache.lucene.search.Query luceneQuery = qb
                .keyword()
                .onFields("description",  "productName")
                .matching(term)
                .createQuery();

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

        // execute search
        List<Product> result = jpaQuery.getResultList();

        return result;
    }

我发现this tutorial 在我的实施过程中很有帮助。

【讨论】:

    猜你喜欢
    • 2015-07-28
    • 2011-11-17
    • 2021-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 2020-09-18
    • 2015-07-25
    相关资源
    最近更新 更多