保存数据到数据库中,同时建立索引
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还快吗?为什么要用它,请求解答,谢谢
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还快吗?为什么要用它,请求解答,谢谢
其搜索代码
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());
}
}我在想,还不如直接写hql语句哦,反正上面的语句最后还是转换成sql
我想问下,上面的写法,岂不是影响性能?那为什么要用hibernate search哦?请求解答