public List<User> search(String keyword) {
long startTime = System.currentTimeMillis();
System.out.println("*****************检索开始**********************");
List<User> userList = new ArrayList<User>();
IndexReader reader;
try {
reader = IndexReader.open(directory);
// 创建IndexSearcher 检索索引的对象,里面要传递上面写入的内存目录对象directory
IndexSearcher searcher = new IndexSearcher(reader);
// 根据搜索关键字 封装一个term组合对象,然后封装成Query查询对象
QueryParser queryParser = new QueryParser(Version.LUCENE_36, "content", analyzer);
Query query = queryParser.parse(keyword);
// 去索引目录中查询,返回的是TopDocs对象,里面存放的就是上面放的document文档对象
TopDocs rs = searcher.search(query, null, 10);
long endTime = System.currentTimeMillis();
System.out.println("总共花费" + (endTime - startTime) + "毫秒,检索到" + rs.totalHits + "条记录。");
User user = null;
for (int i = 0; i < rs.scoreDocs.length; i++) {
// rs.scoreDocs[i].doc 是获取索引中的标志位id, 从0开始记录
Document firstHit = searcher.doc(rs.scoreDocs[i].doc);
user = new User();
user.setId(Long.parseLong(firstHit.get("id")));
user.setName(firstHit.get("name"));
user.setSex(firstHit.get("sex"));
user.setDosomething(firstHit.get("dosometing"));
user.setEmail(firstHit.get("email"));
user.setContent(firstHit.get("content"));
userList.add(user);
// System.out.println("name:" + firstHit.get("name"));
// System.out.println("sex:" + firstHit.get("sex"));
// System.out.println("dosomething:" + firstHit.get("dosometing"));
}
reader.close();
} catch (CorruptIndexException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("*****************检索结束**********************");
return userList;
}
}
Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a9
Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a8
Lucene + Hadoop 分布式搜索运行框架 Nut 1.0a7
Project 2-1: 配置Lucene, 建立WEB查询系统[Ubuntu 10.10]