索引lucene包下面的demo文件。
//设置索引存放位置
Directory dir = FSDirectory.open(Paths.get("c:\\lucene"));
//标准分词器:英语分词器,后面中文需要换中文的分词器实现类
Analyzer analyzer = new StandardAnalyzer();
//写索引的配置
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);
//索引实例,将索引目录、分词器组装到一起
IndexWriter writer = new IndexWriter(dir, iwc);
File[] listFiles = new File("c:\\data").listFiles();
for (File file : listFiles) {
Document doc = new Document();
doc.add(new TextField("fileName", file.getName(), Field.Store.YES));
// doc.add(new TextField("content", "a apple x 128G", Field.Store.YES));
doc.add(new TextField("content", new FileReader(file)));
doc.add(new TextField("fullPath", file.getCanonicalPath(), Field.Store.YES));
writer.addDocument(doc);
System.out.println("被索引的文件:"+file.getCanonicalPath());
}
System.out.println("索引的文件数量:"+writer.numDocs());
writer.close();