简体   繁体   中英

How to retrieve a specific column from a table using hibernate lucene search?

I need to retrieve a specific column using an Hibernate Search query. I tried with projections but that didn't work out. Please help me to achieve this, any advice or suggestion will be appreciated. Here is my query:

     FullTextSession fullTextSession = Search.getFullTextSession(session);
     QueryBuilder b = fullTextSession.getSearchFactory()
            .buildQueryBuilder().forEntity(Company.class).get();
        org.apache.lucene.search.Query luceneQuery =
            b.keyword()
                .onFields("company_name","city")
                .matching("search keyword here!")
                .createQuery();
        org.hibernate.Query fullTextQuery=fullTextSession.createFullTextQuery(luceneQuery).setProjection().setProperties("company_name");       
        List result = fullTextQuery.list();

You can use projections. But before that you must store that column value using store=Store.yes

@Field(store=Store.YES)
private String name;

In the program, you can use projection

Session session = sessionFactory.openSession();

FullTextSession fullTextSession = Search.getFullTextSession(session);
fullTextSession.createIndexer().startAndWait();

QueryBuilder queryBuilder = fullTextSession.getSearchFactory().buildQueryBuilder().forEntity(Employee.class).get();
org.apache.lucene.search.Query luceneQuery = queryBuilder.keyword().onFields("name").matching(name).createQuery();
org.hibernate.search.FullTextQuery fullTextQuery = fullTextSession.createFullTextQuery(luceneQuery, Employee.class);
fullTextQuery.setProjection("name");

List list = fullTextQuery.list();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM