Lucene-Lucene 3.6.1 先排序后分页如何优化性能(见源码)

项目合作 项目合作 主题:1030 回复:2135

Lucene-Lucene 3.6.1 先排序后分页如何优化性能(见源码)

夜无邪 发布于 2017-02-11 字数 2442 浏览 1247 回复 1
public ArrayList queryIndexList(String id1, String id2, String id3, int curpage, int pageSize) {
ArrayList list = new ArrayList();
try {
if (curpage <= 0) {
curpage = 1;
}
if (pageSize <= 0) {
pageSize = 20;
}
this.pageSize = pageSize; //每页记录数
this.currentPage = curpage; //当前页
int start = (curpage - 1) * pageSize;

BooleanQuery bQuery = new BooleanQuery();

if (!"".equals(id1)) {
WildcardQuery w1 = new WildcardQuery(new Term("idml", id1 + "*"));
bQuery.add(w1, BooleanClause.Occur.SHOULD);
}
if (!"".equals(id2)) {

WildcardQuery w1 = new WildcardQuery(new Term("idml", id2 + "*"));
bQuery.add(w1, BooleanClause.Occur.SHOULD);
}
if (!"".equals(id3)) {

WildcardQuery w1 = new WildcardQuery(new Term("idml", id3 + "*"));
bQuery.add(w1, BooleanClause.Occur.SHOULD);
}

int hm = start + pageSize;
TopScoreDocCollector res = TopScoreDocCollector.create(pageSize, false);//TopScoreDocCollector.create(hm, false); 原分页方法
searcher.search(bQuery, res); //第一次查询取出总条数
this.rowCount = res.getTotalHits();
this.pages = (rowCount - 1) / pageSize + 1; //计算总页数
if(rowCount>0){
TopDocs tds = searcher.search(bQuery, rowCount, sort);//第二次查询取全部排序结果 res.topDocs(start, pageSize) 原分页方法
ScoreDoc[] sd = tds.scoreDocs;
int i = 0;
for (ScoreDoc scoreDoc : sd) {
i++;
if (i < start) {
continue;//分页判断
}
if (i > hm) {
break;//分页判断
}
Document doc = searcher.doc(scoreDoc.doc);
list.add(createObj(doc));//封装对象
}
}

} catch (Exception e) {
e.printStackTrace();
}

return list;

}

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

支持 Markdown 语法,需要帮助?