【问题标题】:How to use Hibernate Search with a wildcard query and output the result object list如何使用带有通配符查询的 Hibernate Search 并输出结果对象列表
【发布时间】:2022-01-22 22:01:51
【问题描述】:

我想在 MySQL 数据库中使用 hibernate 搜索的 XML 数据中搜索一个字符串,并打印包含该字符串的数据的结果列表。

【问题讨论】:

    标签: java hibernate search wildcard


    【解决方案1】:

    ..成功了

        public List<Object> listFormSubmissionsBySearch(String searchedString) throws InterruptedException {
    
            Session session = sessionFactory.openSession();
            EntityManager entityManager = session.getEntityManagerFactory().createEntityManager();
    
            FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
            fullTextEntityManager.createIndexer().startAndWait();
    
            QueryBuilder queryBuilder = fullTextEntityManager.getSearchFactory().buildQueryBuilder().forEntity(FormSubmission.class).get();
    
            org.apache.lucene.search.Query wildcardQuery = queryBuilder
                    .keyword()
                    .wildcard()
                    .onField("data") // name of the field in database
                    .matching(searchedString)
                    .createQuery();
    
            List<Object> results = (List<Object>) fullTextEntityManager
                    .createFullTextQuery(wildcardQuery, FormSubmission.class)
                    .setProjection(ProjectionConstants.THIS, ProjectionConstants.SCORE)
                    .getResultList();
    
    // List<Object> flowSubmissions = fullTextEntityManager.createFullTextQuery(wildcardQuery, FlowSubmission.class).getResultList();
    
    
            return results;
        }
    

    【讨论】:

      猜你喜欢
      • 2018-04-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多