lucene 排序 分页

来源:互联网 发布:逆战猎场天赋具体数据 编辑:程序博客网 时间:2024/06/06 20:01
  
  private String searchDir = null;    private static File indexFile = null;   /**     * 读取索引文件     * @return     * @throws Exception     */    private IndexSearcher getSearcher() throws Exception{    searchDir = InitProperties.getValue("luceneFile");//读取配置文件中的文件路径    indexFile = new File(searchDir);        ireader = DirectoryReader.open(FSDirectory.open(indexFile));        return new IndexSearcher(ireader);    }/**     * 中文分词工具     * @return     */    private IKAnalyzer getAnalyzer(){        if(analyzer == null){            return new IKAnalyzer();        }else{            return analyzer;        }    }/**     * 根据页码和分页大小获取上一次的最后一个ScoreDoc     */    private ScoreDoc getLastScoreDoc(int pageIndex,int pageSize,Query query,IndexSearcher searcher,Sort sort) throws IOException {        if(pageIndex==1)return null;//如果是第一页就返回空        int num = pageSize*(pageIndex-1);//获取上一页的数量        TopDocs tds = searcher.search(query, num,sort);        return tds.scoreDocs[num-1];    }/*** 查询列表ajax方法*/@SuppressWarnings("unchecked")public void searchSelect() throws Exception{IndexSearcher isearcher = getSearcher();        Analyzer analyzer =  getAnalyzer();        //使用多字段查询        String [] queryFields = new String[]{"pg_title","author","title","catalog",        "picformat","picbigness","picexplain","year","month","day","area","press",        "picsize","picdpi","framename","camera","shot","aperture","shutter","exposuremode","iso","focallen","sign"};        QueryParser qp = new MultiFieldQueryParser(version,queryFields, analyzer);        Query query = qp.parse(keyword);        //构建排序方式        Sort sort = new Sort(new SortField("id", SortField.Type.INT, true));// false升序true降序         //先获取上一页的最后一个元素 (参数  当前页号,每页显示条数) 注:searchAfter方法添加排序时,本方法必须添加排序        ScoreDoc lastSd = getLastScoreDoc(pageNo,perPageNumber, query, isearcher,sort);        //通过最后一个元素搜索下页的pageSize个元素        TopDocs topdocs = isearcher.searchAfter(lastSd,query,perPageNumber,sort);        long count = 0l;        //获取总条数        count = topdocs.totalHits;        //获取数据        ScoreDoc[] hits = topdocs.scoreDocs;        List<Picture> list = new ArrayList<Picture>();        String filefunction = "com.krkj.hntps.picture";        //装配数据        for (int i = 0; i < hits.length; i++) {        Picture pic = new Picture();            Document doc = isearcher.doc(hits[i].doc);            pic.setId(Long.valueOf(doc.get("id")));            pic.setAuthor(doc.get("author"));            pic.setTitle(doc.get("title"));            list.add(pic);        }               }

参考文档:http://blog.csdn.net/mdcmy/article/details/38167955

                    http://sunney2010.iteye.com/blog/1399490

0 0