hibernate search lucene搜索的用法
jywbd 2010-06-03 10:25:57 保存数据到数据库中,同时建立索引
public void createIndex(ColumnBean column) {
try {
this.getHibernateTemplate().save(column);
Session session = this.getSession();
FullTextSession fullTextSession = Search
.createFullTextSession(session);
fullTextSession.save(column);
} catch (Exception e) {
throw new RuntimeException("create index error!!!");
}
// fullTextSession.index(dep);
}
//////////////////////////////////////////////////////////////////////////
进行搜索
public void searchDate() throws Exception {
Session session = this.getSession();
if (session != null) {
FullTextSession fullTextSession = Search
.createFullTextSession(session);
QueryParser parser = new QueryParser("columnValue",
new StandardAnalyzer());
org.apache.lucene.search.Query luceneQuery = parser
.parse("columnValue:work");
Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,
ColumnBean.class);
List list = hibQuery.list();
System.out.println("test create:" + list.size());
}
}
/////////////////////////////////////////////////////////////////////////////////
进行搜索后,从控制台打印出这些语句
Hibernate: select this_.column_id as column1_0_0_, this_.column_value as column2_0_0_, this_.field_id as field3_0_0_, this_.record_id as record4_0_0_ from dafil this_ where (this_.column_id in (?))
这个搜索函数居然最后返回hql语句,就等于绕了一个大弯,还不如直接写hql吧,我就不明白,hibernate search 集合lucene进行搜索 用法会比直接写hql还快吗?为什么要用它,请求解答,谢谢