Home > Software engineering >  How to use Hibernate Search with a wildcard query and output the result object list
How to use Hibernate Search with a wildcard query and output the result object list

Time:12-26

I want to search for a string in XML data in MySQL database using hibernate search and print the result list of data that contains the string.

CodePudding user response:

..it worked

    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;
    }
  • Related