? Lucene-Lucene的分页查询_蜘蛛资讯网 yb亚博娱乐城,yabo888体育,外围体育投注app
文章故事
首页 | 爱情文章 | 亲情文章 | 友情文章 | 生活随笔 | 校园文章 | 经典文章 | 人生哲理 | 励志文章 | 搞笑文章 | 心情日记 | 英语文章 | 会员中心
当前位置:文章故事>爱情文章>文章内容 经典美文欣赏

文山吩秆广告传媒有限公司_Lucene-Lucene的分页查询

目录

  • 1 Lucene的分页查询
  • 2 代码示例
  • 3 分页查询结果

1 Lucene的分页查询

搜索内容过多时, 需要考虑分页显示, 如:
图片
说明: Lucene的分页查询是在内存中实现的.

2 代码示例

/**
 * 检索索引(实现分页)
 * @throws Exception 
 */
@Test
public void searchIndexByPage() throws Exception {
    // 1. 创建分析器对象(Analyzer), 用于分词
    // Analyzer analyzer = new StandardAnalyzer();
    // 使用ik中文分词器
    Analyzer analyzer = new IKAnalyzer();
    
    // 2. 创建查询对象(Query)
    // bookName:lucene
    // 2.1 创建查询解析器对象
    // 参数一: 指定一个默认的搜索域
    // 参数二: 分析器
    QueryParser queryParser = new QueryParser("bookName", analyzer);
    // 2.2 使用查询解析器对象, 实例化Query对象
    // 参数: 查询表达式
    Query query = queryParser.parse("bookName:lucene");
    
    // 3. 创建索引库目录位置对象(Directory), 指定索引库的位置
    Directory directory = FSDirectory.open(new File("/Users/healchow/Documents/index")); 
    
    // 4. 创建索引读取对象(IndexReader), 用于读取索引
    IndexReader reader = DirectoryReader.open(directory);
    
    // 5. 创建索引搜索对象(IndexSearcher), 用于执行搜索
    IndexSearcher searcher = new IndexSearcher(reader);
    
    // 6. 使用IndexSearcher对象, 执行搜索, 返回搜索结果集TopDocs
    // search方法: 执行搜索
    // 参数一: 查询对象
    // 参数二: n - 指定返回排序以后的搜索结果的前n个
    TopDocs topDocs = searcher.search(query, 10);
    
    // 7. 处理结果集
    // 7.1 打印实际查询到的结果数量
    System.out.println("实际查询的结果数量: "+topDocs.totalHits);
    // 7.2获取搜索的结果数组
    // ScoreDoc中: 有我们需要的文档的id, 有我们需要的文档的评分
    ScoreDoc[] scoreDocs = topDocs.scoreDocs;
    
    // 增加分页实现 ========= start
    // 当前页
    int page = 2;
    // 每页的大小
    int pageSize = 2;
    // 计算记录起始数
    int start = (page - 1) * pageSize;
    // 计算记录终止数 -- 得到数据索引与实际结果数量中的最小值, 防止数组越界
    // 数据索引: start + pageSize
    // 实际的结果数量: scoreDocs.length
    int end = Math.min(start + pageSize, scoreDocs.length);
    // 增加分页实现 ========= end
    
    for(int i = start; i < end; i++){
        System.out.println("= = = = = = = = = = = = = = = = = = =");
        // 获取文档Id和评分
        int docId = scoreDocs[i].doc;
        float score =scoreDocs[i].score;
        System.out.println("文档Id: " + docId + " , 文档评分: " + score);
        // 根据文档Id, 查询文档数据 -- 相当于关系数据库中根据主键Id查询
        Document doc = searcher.doc(docId);
        
        System.out.println("图书Id: " + doc.get("bookId"));
        System.out.println("图书名称: " + doc.get("bookName"));
        System.out.println("图书价格: " + doc.get("bookPrice"));
        System.out.println("图书图片: " + doc.get("bookPic"));
        System.out.println("图书描述: " + doc.get("bookDesc"));
    }
    // 8. 释放资源
    reader.close();
}

3 分页查询结果

这里查询到的结果共有3条, 所以第2页只有一条结果:
图片

2 dai ma shi li jian suo suo yin shi xian fen ye throws Exception Test public void searchIndexByPage throws Exception 1. chuang jian fen xi qi dui xiang Analyzer, yong yu fen ci Analyzer analyzer new StandardAnalyzer shi yong ik zhong wen fen ci qi Analyzer analyzer new IKAnalyzer 2. chuang jian cha xun dui xiang Query bookName: lucene 2. 1 chuang jian cha xun jie xi qi dui xiang can shu yi: zhi ding yi ge mo ren de sou suo yu can shu er: fen xi qi QueryParser queryParser new QueryParser" bookName", analyzer 2. 2 shi yong cha xun jie xi qi dui xiang, shi li hua Query dui xiang can shu: cha xun biao da shi Query query queryParser. parse" bookName: lucene" 3. chuang jian suo yin ku mu lu wei zhi dui xiang Directory, zhi ding suo yin ku de wei zhi Directory directory FSDirectory. open new File" Users healchow Documents index" 4. chuang jian suo yin du qu dui xiang IndexReader, yong yu du qu suo yin IndexReader reader DirectoryReader. open directory 5. chuang jian suo yin sou suo dui xiang IndexSearcher, yong yu zhi xing sou suo IndexSearcher searcher new IndexSearcher reader 6. shi yong IndexSearcher dui xiang, zhi xing sou suo, fan hui sou suo jie guo ji TopDocs search fang fa: zhi xing sou suo can shu yi: cha xun dui xiang can shu er: n zhi ding fan hui pai xu yi hou de sou suo jie guo de qian n ge TopDocs topDocs searcher. search query, 10 7. chu li jie guo ji 7. 1 da yin shi ji cha xun dao de jie guo shu liang System. out. println" shi ji cha xun de jie guo shu liang: " topDocs. totalHits 7. 2 huo qu sou suo de jie guo shu zu ScoreDoc zhong: you wo men xu yao de wen dang de id, you wo men xu yao de wen dang de ping fen ScoreDoc scoreDocs topDocs. scoreDocs zeng jia fen ye shi xian start dang qian ye int page 2 mei ye de da xiao int pageSize 2 ji suan ji lu qi shi shu int start page 1 pageSize ji suan ji lu zhong zhi shu de dao shu ju suo yin yu shi ji jie guo shu liang zhong de zui xiao zhi, fang zhi shu zu yue jie shu ju suo yin: start pageSize shi ji de jie guo shu liang: scoreDocs. length int end Math. min start pageSize, scoreDocs. length zeng jia fen ye shi xian end for int i start i lt end i System. out. println" " huo qu wen dang Id he ping fen int docId scoreDocs i. doc float score scoreDocs i. score System. out. println" wen dang Id: " docId " , wen dang ping fen: " score gen ju wen dang Id, cha xun wen dang shu ju xiang dang yu guan xi shu ju ku zhong gen ju zhu jian Id cha xun Document doc searcher. doc docId System. out. println" tu shu Id: " doc. get" bookId" System. out. println" tu shu ming cheng: " doc. get" bookName" System. out. println" tu shu jia ge: " doc. get" bookPrice" System. out. println" tu shu tu pian: " doc. get" bookPic" System. out. println" tu shu miao shu: " doc. get" bookDesc" 8. shi fang zi yuan reader. close 3 fen ye cha xun jie guo zhe li cha xun dao de jie guo gong you 3 tiao, suo yi di 2 ye zhi you yi tiao jie guo:

版权声明

作者: ma_shoufeng(马瘦风)

出处: 博客园 马瘦风的博客

您的支持是对博主的极大鼓励, 感谢您的阅读.

本文版权归博主所有, 欢迎转载, 但未经博主同意必须保留此段声明, 且在文章页面明显位置给出原文链接, 否则博主保留追究法律责任的权利.

当前文章:http://www.gas-360.com/z5xzm4caj/354025-992686-22506.html

发布时间:09:06:17

香港马会资料??港六和彩开奖??4766香港老地方百度??www.44494.com??今晚开码结果??455155.com??手机最快报码现场直播??跑狗玄机图??www.63307.com??管家婆彩图心水坛论??

Copyright ? 2007-2014 蜘蛛资讯网 版权所有.情感文章,散文随笔,美文故事在线阅读