经过前面的简单理论介绍,相信大家对搜索引擎lucene有个简单的了解。前面我们也提到过在lucene中主要包括索引和搜索这两大方面的组件。今天我们我们就通过一个简单的实例来看一下lucene给我们提供的有关这两个组件的简单用法。
一:创建索引
在用lucene搜索之前,我们首先要做的就是是创建索引,只有有索引了,我们才有了搜索的对象,下面我们就根据一个创建索引的小demo来一步步分析一下创建索引的步骤:
public class Indexer {
private IndexWriter writer = null;
public Indexer(String indexDir) throws Exception {
Directory dir = FSDirectory.open(new File(indexDir));//打开保存索引目录
writer = new IndexWriter(dir, new StandardAnalyzer(Version.LUCENE_30),
true, IndexWriter.MaxFieldLength.UNLIMITED);//创建lucene IndexWriter,创建索引工具
}
public void close() throws Exception
{
writer.close();
}
public int index(String dataDir, FileFilter filter) throws Exception {
File[] files = new File(dataDir).listFiles();
for (File file : files) {//遍历文件目录下所有txt文件,把文件加入索引
if(!file.isDirectory() && !file.isHidden() && file.exists()
&& (filter == null || filter.accept(file))) {
indexFile(file);
}
}
return writer.numDocs();
}
private Document getDocument(File f) {
Document doc = new Document();
try {
doc.add(new Field("content", new FileReader(f)));
doc.add(new Field("fileName", f.getName(), Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("filePath", f.getCanonicalPath(),
Field.Store.YES, Field.Index.NOT_ANALYZED));
} catch (Exception e) {
e.printStackTrace();
}
return doc;
}
public void indexFile(File f) throws Exception {
System.out.println("make indexfile is " + f.getCanonicalPath());
Document doc = getDocument(f);//创建