lucene-分页
2018-01-29 22:40
106 查看
分页也是通过查询来搞
修改findIndex方法
OK,测试
首先往Lucene里存点数据
注意,这样存的话可能会报错,要将LuceneUtils的一个方面改成这样。因为原来是有一个为空判断的。只有当数据库为空的时候才会插入数据。在我们插入多条数据时,插入过一条,再插入第二条数据库就不再为空,也就导致了插入失败。所以插入数据的时候,要将这个为空判断改掉;
插入数据完毕之后,可以测试,从第0条数据开始,取5条:
修改findIndex方法
//int firstResult,int maxResult都是分页所需要的参数 //firstResult:从哪条数据开始 //maxResult:最多取几条 public void findIndex(String keywords,int firstResult,int maxResult) throws Exception{ IndexSearcher indexSearcher = LuceneUtils.getIndexSearcher(); String fields[] = {"title","content"}; QueryParser queryParser = new MultiFieldQueryParser(LuceneUtils.getVersion(),fields,LuceneUtils.getAnalyzer()); Query query = queryParser.parse(keywords); //分页开始 //使用lucene提供的api进行操作... TopDocs topDocs = indexSearcher.search(query, firstResult + maxResult); ScoreDoc scoreDocs[] = topDocs.scoreDocs; //判断:scoerDocs的length(实际取出来的数量..)与firstResult + maxResult的取值 int endResult = Math.min(scoreDocs.length, firstResult + maxResult); for (int i = firstResult; i < endResult; i++) { int docID = scoreDocs[i].doc; 4000 Document document = indexSearcher.doc(docID); System.out.println("id===" + document.get("id")); System.out.println("title===" + document.get("title")); System.out.println("content===" + document.get("content")); System.out.println("url===" + document.get("url")); System.out.println("author===" + document.get("author")); } } |
首先往Lucene里存点数据
@Test public void testCreate() throws IOException{ for (int i = 1; i <= 25; i++) { Article article = new Article(); article.setId(i); article.setTitle("一定要有梦想,万一实现了呢"); article.setContent("这句话太矫情了"); article.setUrl("http://www.tianmao.com"); article.setAuthor("马云"); luceneDao.addIndex(article); } } |
/** * @return 返回用于操作索引的对象... * @throws IOException */ public static IndexWriter getIndexWriter() throws IOException{ indexWriter = new IndexWriter(directory,indexWriterConfig); return indexWriter; } |
@Test public void testSearcher() throws Exception{ //limit(0,10) //从20开始,取10条 luceneDao.findIndex("梦想",0,5); } |
查询带有"梦想"的数据: |
相关文章推荐
- lucene.net 搜索及分页
- Lucene系列:(11)异步分页
- lucene-分页
- lucene两个分页操作
- 【转帖】[Lucene.Net] 多线程操作建议和[Lucene.Net] 分页显示
- lucene-分页
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- Lucene的多域查询、结果中查询、查询结果分页、高亮查询结果和结果评分
- lucene的多个字段搜索以及分页并高亮显示
- lucene-分页
- lucene4.7 分页
- 转 lucene3搜索引擎,索引建立搜索排序分页高亮显示, IKAnalyzer分词
- Lucene4.3.1 两种分页方式
- lucene的两种分页操作
- lucene的排序过滤和分页
- lucene-分页
- lucene.net 3.0.3、结合盘古分词进行搜索的小例子(分页功能)
- lucene结果集的分页处理(百度 分页 方式)(转)