我把数据库的category表用lucene索引, 但有一个要求没有输入关键字时, 怎么把所有结果查出来.
private static List<Category> doPagingSearch(Searcher searcher, String keyWord, int hitsPerPage) throws IOException { List<Category> catList = new ArrayList<Category>();
try {
// Create the QueryParser instance
QueryParser descriptionQP = new QueryParser(Version.LUCENE_30, "description", getAnalyzer()); String cleanQueryString = escapeBadCharacters(keyWord);
Query descriptionQuery = descriptionQP.parse(cleanQueryString); // Collect enough docs to show 5 pages
TopScoreDocCollector collector = TopScoreDocCollector.create(5 * hitsPerPage, false); searcher.search(descriptionQuery, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; for (int i = 0; i < hits.length; i++) {
Document doc = searcher.doc(hits[i].doc);
Category category = new Category();
category.setCatId(Long.valueOf(doc.get("catId")));
category.setDescription(doc.get("description"));
category.setParentCatId(Long.valueOf(doc.get("parentCatId")));
catList.add(category);
}
int numTotalHits = collector.getTotalHits();
System.out.println(numTotalHits + " total matching documents"); } catch (ParseException e) {
logger.severe("Search Query" + e.getMessage());
} return catList;
}
private static List<Category> doPagingSearch(Searcher searcher, String keyWord, int hitsPerPage) throws IOException { List<Category> catList = new ArrayList<Category>();
try {
// Create the QueryParser instance
QueryParser descriptionQP = new QueryParser(Version.LUCENE_30, "description", getAnalyzer()); String cleanQueryString = escapeBadCharacters(keyWord);
Query descriptionQuery = descriptionQP.parse(cleanQueryString); // Collect enough docs to show 5 pages
TopScoreDocCollector collector = TopScoreDocCollector.create(5 * hitsPerPage, false); searcher.search(descriptionQuery, collector); ScoreDoc[] hits = collector.topDocs().scoreDocs; for (int i = 0; i < hits.length; i++) {
Document doc = searcher.doc(hits[i].doc);
Category category = new Category();
category.setCatId(Long.valueOf(doc.get("catId")));
category.setDescription(doc.get("description"));
category.setParentCatId(Long.valueOf(doc.get("parentCatId")));
catList.add(category);
}
int numTotalHits = collector.getTotalHits();
System.out.println(numTotalHits + " total matching documents"); } catch (ParseException e) {
logger.severe("Search Query" + e.getMessage());
} return catList;
}
解决方案 »
- element的用法
- Uncaught exception java/lang/NoclassDefFoundError 问题
- Java泛型中的擦除与多态性是如何冲突的?
- 求最简单的散列方法,字串转int
- 请要看免费电影和听免费音乐的网友到我网站上去下载,都能下载的哦!绝不骗人!不好的话,你用鸡蛋砸我好了!
- 怎么取得文件类型如:text/html
- 请问有哪位大侠有jpaser的教程?
- 很多线程同时写数据库的问题
- 求问:一个关于如何写升降序排列的代码编写问题?
- 关于java开发工具和application server哪个好的大讨论
- 关于java语音的问题
- 使用URL类下载一个图片文件抛ClientAbortException
现在要求显示所有结果