2013-07-08期-范围查询及结果排序
2014-02-08 09:19
344 查看
调整了SearchAction.java代码
降序结果页面
![](http://www.liutime.com/img/286.jpg)
升序结果页面
public String searcheContent() throws Exception { searcheResult = new ArrayList<ContentObject>(); try{ //当查询条件不存在时,查询数据库 if(StringUtils.isEmpty(keyWord)){ searcheResult = contentService.getContentList(); return SUCCESS; } Version v = Version.LUCENE_43; //取得查询对象 IndexReader[] readers = SearchObject.getInstance().getSearcherReads(); MultiReader mReaders = new MultiReader(readers); IndexSearcher indexSearch = new IndexSearcher(mReaders); Analyzer analyzer =new StandardAnalyzer(v); //创建boolean查询 BooleanQuery query = new BooleanQuery(); String[] field = { "title", "content"}; BooleanClause.Occur[] flags = new BooleanClause.Occur[2]; flags[0] = BooleanClause.Occur.SHOULD; flags[1] = BooleanClause.Occur.SHOULD; Query query1 = MultiFieldQueryParser.parse(v, QueryParser.escape(keyWord), field, flags, analyzer); //query.add(query1, Occur.MUST); //日期范围查询 Calendar calendar = Calendar.getInstance(); //查询两个月以内的数据 calendar.add(Calendar.DAY_OF_MONTH, -60); Date lastMonth = calendar.getTime(); query.add(NumericRangeQuery.newLongRange("createDate", lastMonth.getTime(), new Date().getTime(), true, true), Occur.MUST); //根据ID范围查询 //query.add(NumericRangeQuery.newIntRange("id", 0, 20, true, true), Occur.MUST); //true为降序,false为升序 SortField field1 = new SortField("createDate", SortField.Type.LONG,true); Sort sort = new Sort(field1); //取得查询结果 TopDocs topDocs = indexSearch.search(query,100000, sort); //TopScoreDocCollector topCollector = TopScoreDocCollector.create(10000, true,sort); // indexSearch.search(query, topCollector); // TopDocs topDocs = topCollector.topDocs(); int resultCount=topDocs.totalHits; for(int i=0;i<resultCount;i++){ Document doc = indexSearch.doc(topDocs.scoreDocs[i].doc); //转换Document对象为内容对象 //ContentObject content = PackContentObject.convertDocToContent(doc); ContentObject content = PackContentObject.convertDocToContent(doc, query1, analyzer); //加入到结果列表 返回给前台页面获取 searcheResult.add(content); } }catch(Exception e){ e.printStackTrace(); } return SUCCESS; }
降序结果页面
![](http://www.liutime.com/img/286.jpg)
升序结果页面
![](http://www.liutime.com/img/287.jpg)
相关文章推荐
- 微信搜一搜迈出新的一步,好戏来了
- 搜狗百度360市值齐跌:搜索引擎们陷入集体焦虑?
- 本人即将筹备败家日志,敬请期待!
- IE:使用搜索助手
- 用SQL建立索引的方法步骤
- SQL效率提升之一些SQL编写建议并有效利用索引
- SQLSERVER的非聚集索引结构深度理解
- SQL Server误区30日谈 第8天 有关对索引进行在线操作的误区
- SQL Server 索引介绍
- SqlServer 索引自动优化工具
- 优化 SQL Server 索引的小技巧
- sqlserver 索引的一些总结
- mysql中索引使用不当速度比没加索引还慢的测试
- Sql Server 查询性能优化之走出索引的误区分析
- sqlserver索引的原理及索引建立的注意事项小结
- 基于文本的搜索
- 使用Sphinx对索引进行搜索
- asp 多关键词搜索的简单实现方法
- 关于Sphinx创建全文检索的索引介绍