qq_charm
2018-05-09 07:26
采纳率: 25%
浏览 665
已采纳

java lucene的搜索和翻页效率?

lucene上千万的数据翻页怎么这么慢啊,基本上要5s-6s。前面的页数还比较快,越到后面的页数翻页越慢。
贴上代码:
方式一:

 public static void searchPage(int pageIndex,int pageSize) throws IOException, ParseException{
        Date start=new Date();
        Directory dir=FSDirectory.open(Paths.get("D:\\FCJS\\FCJS_bigdata_lucene5.3.0"));
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher=new IndexSearcher(reader);
        Analyzer analyzer=new IKAnalyzer(false);//false细粒度分词,true智能分词
        QueryParser parser=new QueryParser("all", analyzer);
        Query query=parser.parse("all");
        TopDocs tds = searcher.search(query,Integer.MAX_VALUE);
        ScoreDoc[] sds = tds.scoreDocs;
        int page_start = (pageIndex-1)*pageSize;  
        int page_end = pageIndex*pageSize;
        for(int i=page_start;i<page_end;i++) {  
            Document doc = searcher.doc(sds[i].doc);  
            System.out.println(sds[i].doc+":"+doc.get("ArticleID"));  
        }
        Date end=new Date();
        System.out.println("耗时:"+(end.getTime()-start.getTime())+"ms");
    }

方式二:

 private static void searchPageByAfter(int pageIndex,int pageSize) throws IOException, ParseException{
        Date start=new Date();
        Directory dir=FSDirectory.open(Paths.get("D:\\FCJS\\FCJS_bigdata"));
        IndexReader reader = DirectoryReader.open(dir);
        IndexSearcher searcher=new IndexSearcher(reader);
        Analyzer analyzer=new IKAnalyzer(false);//false细粒度分词,true智能分词
        QueryParser parser=new QueryParser("all", analyzer);
        Query query=parser.parse("all");
        //获取上一页的最后一个元素  
        ScoreDoc lastSd = getLastScoreDoc(pageIndex, pageSize, query, searcher); 
      //通过最后一个元素去搜索下一页的元素  
        TopDocs tds = searcher.searchAfter(lastSd,query, pageSize);
        for(ScoreDoc sd:tds.scoreDocs) {  
            Document doc = searcher.doc(sd.doc);  
            System.out.println(sd.doc+":"+doc.get("ArticleID"));  
        } 
        Date end=new Date();
        System.out.println("耗时:"+(end.getTime()-start.getTime())+"ms");
    }

3条回答 默认 最新

相关推荐 更多相似问题