getHibernateTemplate().execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException
{
try {
FullTextSession fullSession = Search.createFullTextSession(session);
QueryParser parser = new QueryParser("iartTitle",new StandardAnalyzer());
Query luceneQuery = parser.parse("iartTitle:" + "重庆");
FullTextQuery query = fullSession.createFullTextQuery( luceneQuery, InfoArticle.class );
List<InfoArticle> list = query.list();
return list;
} catch (ParseException e) {
e.printStackTrace();
return null;
}
}
});
这是我的查询代码,但是只能查一个字段,怎样多条件联合查询
解决方案 »
- 如何取得 session中的bean
- java 编译的高斯列主元消去法 代码
- 关于外键映射?
- eclise在Debug时错误
- tomcat下,servlet初始化时,执行init()2次,怎么回事呢?
- 找工作好痛苦啊,没工作经验没人要,各位大哥你们都是怎么过来得啊
- javaMail取不出来Flag,大侠们帮帮忙吧!!
- 如何用java开发邮件服务器
- 讨论:两个<Thinking in Java>译本的比较.......
- mybatis物理分页rowbounds临时表别名问题
- hibernate中一对多级联保存一个很大的List对象,怎么做才降低内存损耗。
- 求助使用过滤器过滤:任意输入一个jsp地址,都能自动跳转到只有端口不同的该地址!解决结贴,在线等。。。
这个是讲的hibernate关于search的hql用法我的问题是hibernateSearch(全文检索)的hql用法不过同样谢谢你的回答
方法一:利用hibernate的方法设置参数
public List<IptvAction> queryByIptvAction() {
String hql = "from table where cat = 5 and dateTime between ? and ? group by code order by count(*) desc";
return this.list(hql, 0, 6, new Object[] { ConvertDate.getOldDate(),
ConvertDate.getNewDate() });
}方法二:直接写拼接语句
String hql = "from table a where a.code="+ code +" and a.uid="+id;
return this.getHibernateTemplate().find(hql);
这位大虾的回答也是讲的hibernate关于search的hql用法 你没理解我的意思,我明白,因为habernateSearch(全文检索)接触的人不多
全文检索?
你是想一个字段但是要好多个值?
例如 iartTitle 既为:重庆又为广州?
try {
List<T> results = getHibernateTemplate().findByExample(instance);
return results;
} catch (RuntimeException re) {
throw re;
}
}
instance 有什么属性就给什么属性赋值即可。
title,content,
我现在只知道单独的检索,我想联合条件检索HibernateSearch检索不查数据库,而是直接找索引(索引通过habernate的增、删、改建立)
可以
不是你不明白需求
是因为你没用过hibernateSearch
所以看不明白
你看我贴的代码就知道这不是hibernate的基本查询
发现我确实没接触过。
确实不知道这个东东。
而且中文介绍好像比较少啊?
搜出来都是English.....
assertNotNull(session);
QueryParser parser = new QueryParser("name", new StopAnalyzer());
org.apache.lucene.search.Query luceneQuery = parser
.parse("name:Kevin");
Query hibQuery = fullTextSession.createFullTextQuery(luceneQuery,
Employee.class); 不明白楼主你要的联合查询是撒意思啊?
是一个字段的联合?还是多个字段啊?不过估计你说了我还是不明白。呵呵。。
继续关注。
* 创建索引
* @param path
*/
public void createIndex(String path){
try {
IndexWriter writer = new IndexWriter(path,new StandardAnalyzer(),true);
Document docA = new Document();
//相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置
//Field.Store 是否覆盖原来的索引文件,而不是重新建一个
Field fieldA = new Field("content","搜索引擎",Field.Store.YES,Field.Index.TOKENIZED);
//我们把列(fieldA)加到某一行(docA)中
docA.add(fieldA);
docA.add(new Field("title","你好中国",Field.Store.YES,Field.Index.TOKENIZED));
docA.add(new Field("content","欢迎你llying",Field.Store.YES,Field.Index.TOKENIZED));
docA.add(new Field("lastModifyTime","2008-9-17",Field.Store.YES,Field.Index.TOKENIZED));
docA.add(new Field("testCapital","HelloWangzi",Field.Store.YES,Field.Index.TOKENIZED));
docA.add(new Field("testAndOr","test and",Field.Store.YES,Field.Index.TOKENIZED));
Document docB = new Document();
//相当于数据库中列的概念,因此第一个参数是列名,第二个参数是列的值,最后两个参数是enum类型的(JDK1.5),对创建的索引的设置
Field fieldB = new Field("content","创建索引",Field.Store.YES,Field.Index.TOKENIZED);
//我们把列(fieldA)加到某一行(docA)中
docB.add(fieldB);
docB.add(new Field("title","你好世界",Field.Store.YES,Field.Index.TOKENIZED));
docB.add(new Field("content","欢迎加入jee高级开发群46176507",Field.Store.YES,Field.Index.TOKENIZED));
docB.add(new Field("lastModifyTime","2008-9-6",Field.Store.YES,Field.Index.TOKENIZED));
docB.add(new Field("testCapital","hellowangZi",Field.Store.YES,Field.Index.TOKENIZED));
docB.add(new Field("testAndOr","test or",Field.Store.YES,Field.Index.TOKENIZED));
writer.addDocument(docA);
writer.addDocument(docB);
//如果对海量数据进行创建索引的时候,需要对索引进行优化,以便提高速度
writer.optimize();
//跟数据库类似,打开一个连接,使用完后,要关闭它
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return (Wrapper) this.baseDao.execute(new HibernateCallback(){
public Object doInHibernate(Session session) throws HibernateException, SQLException{
// 将SESSION转换成lucence session,执行全文检索
FullTextSession fullSession = Search.getFullTextSession(session);
//Map<String, String> search = page.getMSearch();
StringBuffer searchQuery = new StringBuffer();
Wrapper wrap = null;
Query luceneQuery = null;
QueryParser parser = new QueryParser("dboSourceName", new StandardAnalyzer());
try {
searchQuery.append("( dboSourceName:").append(page.getMSearch().get("contantion"));//书名
searchQuery.append(" OR dboAuthor:").append(page.getMSearch().get("contantion"));//作者
searchQuery.append(" OR dboSummary:").append(page.getMSearch().get("contantion"));//摘要
searchQuery.append(" OR dboKeyword:").append(page.getMSearch().get("contantion"));//关键词
searchQuery.append(") AND ddbId:").append(page.getMSearch().get("ddbId"));//资源库ID
luceneQuery = parser.parse(searchQuery.toString());
FullTextQuery query = fullSession.createFullTextQuery(luceneQuery, DigitalBookDetail.class);
//总记录数
int size = query.getResultSize();
query.setFirstResult(page.getStart());
query.setMaxResults(page.getLimit());
List<DigitalBookDetail> books = query.list();
wrap = new Wrapper(books,String.valueOf(size));
} catch (ParseException e) {
e.printStackTrace();
}
return wrap;
}
});
}这就是加条件的
终于解决了