How to retrieve a specific column from a table using hibernate lucene search
To retrieve a specific column from a table using hibernate lucene search we can use projections.
Jars required are (same version or lower version)
hibernate-core-5.2.7.Final
hibernate-search-5.7.0.Final
But before that we must store that column value using store=Store.yes
@Indexed
@Table
@Entity
Public class Employee{
@Field(store=Store.YES)
private String name;
// other parameters
//setter & getter methods
}
Note: without this fileld store=Store.yes we cant retrieve the values.
This attribute will store values into index files created by lucene.
In the program, wecan use projection to get all values from that column
public class EmployeeDetails {
@Autowired
SessionFactory sessionFactory;
public List getEmployeeDetails {
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();
return list;
}
}
This list will contain name values from database table.
Nice article.
ReplyDeleteWish u all d best for your future...