近日需要做一下搜索的功能,之前就听说过hibernate search,所以就决定采用这个。
实现了如下的功能,在多域中搜索关键字,并且在以id排序,满足一些状态值。
而且前台可以再次通过过滤器进行二次筛选。
还需要在以后扩展的是高亮显示关键字,还有分词好像没有实现,对象时间怎么关联起来还没找到相关事例资料。服务器重启会丢失索引文件,现在必须重启服务后手动索引。
//手动建索引
public void createHernateIndex() {
FullTextSession s = Search.createFullTextSession(getSession());
List<Product> products = find("from Product");
long startTime = new Date().getTime();
for (Product product : products) {
System.out.println("File " + product.getProTitle() + "正在被索引....");
s.index(product);
}
long endTime = new Date().getTime();
System.out.println("一共" + products.size() + ",这花费了"
+ (endTime - startTime) + " 毫秒来把文档增加到索引里面去!");
}
//搜索
public Page proQuery(String words,String cartype,String address,int pageNo, int pageSize) throws Exception {
FullTextSession s = Search.createFullTextSession(getSession());
// MultiFieldQueryParser parser = new MultiFieldQueryParser(
// new String[] { "proName" }, new StandardAnalyzer());
// Query luceneQuery = parser.parse(words);
// Query luceneQuery = MultiFieldQueryParser.parse(new
// String[]{words},new String[]{"proName","pro_state"},new
// StandardAnalyzer());
Query luceneQuery = MultiFieldQueryParser.parse(new String[] { words,
words,words }, new String[] { "proTitle", "proDescn","car_type" },
new BooleanClause.Occur[] { Occur.SHOULD, Occur.SHOULD,Occur.SHOULD },
new StandardAnalyzer());
FullTextQuery query = s.createFullTextQuery(luceneQuery, Product.class);
// 排序
Sort sort = new Sort(new SortField[] { new SortField("id",
SortField.INT, true) });
// 过滤1
QueryParser qp1 = new QueryParser("proStatus", new StandardAnalyzer());
Query fquery = qp1.parse("2");
// 过滤2
QueryParser qp2 = new QueryParser("pro_type", new StandardAnalyzer());
Query fquery2 = qp2.parse("3");
BooleanQuery bqf = new BooleanQuery();
bqf.add(fquery, BooleanClause.Occur.MUST);
bqf.add(fquery2, BooleanClause.Occur.MUST_NOT);
// 过滤3
if (!cartype.equalsIgnoreCase("")) {
QueryParser qp3 = new QueryParser("car_type",
new StandardAnalyzer());
Query fquery3 = qp3.parse(cartype);
bqf.add(fquery3, BooleanClause.Occur.MUST);
}
// 过滤4
if (!address.equalsIgnoreCase("")) {
QueryParser qp4 = new QueryParser("proOrigin",
new StandardAnalyzer());
Query fquery4 = qp4.parse(address);
bqf.add(fquery4, BooleanClause.Occur.MUST);
}
QueryFilter qf = new QueryFilter(bqf);
query.setSort(sort);
query.setFilter(qf);
long totalCount = query.list().size();
if (totalCount < 1)
return new Page();
int startIndex = Page.getStartOfPage(pageNo, pageSize);
List<Product> list = query.setFirstResult(startIndex).setMaxResults(
pageSize).list();
return new Page(startIndex, totalCount, pageSize, list);
}
分享到:
相关推荐
hibernate search全文索引的创建与查询实例 hibernate-search-analyzers-4.1.1.Final.jar
Hibernate Search is a library providing full-text search capabilities to Hibernate. It opens doors to more human friendly and efficient search engines while still following the Hibernate and Java ...
作用在于对数据表中某些内容庞大的字段(如声明为text的字段)建立全文索引,这样通过hibernate search就可以对这些字段进行全文检索后获得相应的POJO,从而加快了对内容庞大字段进行模糊搜索的速度(sql语句中like...
hibernateSearch 列子源代码
hibernate-search, Hibernate Search Hibernate 搜索版本:5.8.0. Final - 13-09-2017描述针对Java对象的全文搜索这个项目提供 Hibernate ORM和全文索引服务( 如 Apache Lucene和 Elasticsearch
使用hibernate search实现全文检索和文档管理功能: 1 全文检索 2 手动生成索引 3 文档上传(自动建立索引) 4 文档更新(自动建立索引) 5 文档删除 使用说明: 1 需要先恢复数据库 searchDB_2008sqlserver.bak 2 ...
hibernate Search in action
hibernate search 开发环境的搭建。
hibernate search 里面有关于代码的详细说明 请看readme txt 有相关jar直接运行
Hibernate Search 4.4.0.Final API 帮助文档
HibernateSearch的使用方法
hibernate_search.pdf
使用 hibernate-search 实现全文检索及文档管理的例子 1 全文检索 2 手动重新建立索引 3 文档上传(自动建立索引) 4 文档更新(自动建立索引) 5 文档删除
压缩包包括项目完整代码,详细说明和项目结构图,效果图 项目实现了分页和高亮显示 MAVEN项目:HibernateSearchDemo IDE :MyEclipse jdk :1.6 数据库 :MySql ...hibernate search 版本 4.4.1.Final
hibernate search源码 有助于hibernate search源码的研究和学习
hibernate-search-3.4.1 api chm
基于Spring的Hibernate Search全文检索功能示例 实例下载
Hibernate官方提供的最新版本hibernate-search-4.5.1.Final-dist.zip hibernate-search-4.5.1.Final-dist\hibernate-search-4.5.1.Final\dist\lib required所有包和provided的jta-1.1.jar导入项目工程下lib即可
Hibernate Search in Action introduces both the principles of enterprise search and the implementation details a Java developer will need to use Hibernate Search effectively. This book blends the ...