需求,做一个桌面搜索小工具,将制定的目录下的文件,生成索引,提供一个搜索界面,输入关键字,在内容里搜索匹配的文件,列出文件信息:
点击查看项目主页
step1:构建索引:
图片:
代码:
/**
* 根据指定的文件/目录生成索引
*
* @param file
* @throws Exception
*/
private void createIndex(File file) throws Exception {
if (file.isDirectory()) {
File[] files = file.listFiles();
for (File child : files) {
createIndex(child);
}
} else {
String fileName = file.getName();
String extName = fileName.substring(fileName.lastIndexOf(".") + 1)
.toLowerCase();
if ("txt".equals(extName) || "properties".equals(extName)
|| "java".equals(extName) || "jsp".equals(extName)
|| "js".equals(extName) || "css".equals(extName)) {
logger.log(Level.INFO, "索引:" + file.getAbsolutePath());
Document doc = new Document();
doc.add(new Field("id", String.valueOf(new Date().getTime()),
Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS));
doc.add(new Field("filename", file.getName(), Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("filepath", file.getAbsolutePath(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
doc.add(new Field("content", IOUtils.toString(
new FileInputStream(file), "UTF-8"), Field.Store.NO,
Field.Index.ANALYZED));
doc.add(new NumericField("inputdate", Field.Store.YES, true)
.setLongValue(new Date().getTime()));
indexWriter.addDocument(doc);
}
}
}
- step2:进行搜索,返回符合条件的前50条记录:
图片:
代码:
/**
* 根据制定的关键字查询内容匹配的项目
* @param keyword
* @return
* @throws Exception
*/
public List<ResultItem> query(String keyword) throws Exception {
IndexSearcher searcher = new IndexSearcher(FSDirectory.open(new File(
Contants.indexDir)), true);
searcher.setDefaultFieldSortScoring(true, false);
QueryParser parser = new QueryParser(Version.LUCENE_30, "content",
new StandardAnalyzer(Version.LUCENE_30));
Query query = parser.parse(keyword);
org.apache.lucene.search.Sort sort = new org.apache.lucene.search.Sort(
new SortField("updatetime", SortField.LONG, true));
TopDocs result = searcher.search(query, null, Integer.MAX_VALUE, sort);
ScoreDoc[] docs = result.scoreDocs;
System.out.println(docs.length);
List<ResultItem> list = new ArrayList<ResultItem>();
for (int i = 0; i < (docs.length > 50 ? 50 : docs.length); i++) {
Document doc = searcher.doc(docs[i].doc);
ResultItem item = new ResultItem();
item.setId(doc.getField("id").stringValue());
item.setFileName(doc.getField("filename").stringValue());
item.setFilePath(doc.getField("filepath").stringValue());
item.setUpdateTime(new Date(Long.valueOf(doc.getField("inputdate")
.stringValue())));
list.add(item);
logger.info(item.toString());
}
return list;
}