//创建索引
@Test
public void createIndex() throws Exception{
//1.创建一个Directory对象,指定索引库保存保存的位置
/*把索引库保存在内存中
Directory directory = new RAMDirectory();
*/
//把索引库保存在磁盘
Directory directory = FSDirectory.open(new File("E:\\Java_Study\\Lucene\\index").toPath());
//2.基于Directory对象,创建一个IndexWriter对象
IndexWriterConfig config = new IndexWriterConfig(new IKAnalyzer());
IndexWriter indexWriter = new IndexWriter(directory,config);
//3.读取磁盘上的文件,对应每个文件创建一个文档对象
File dir = new File("F:\\searchsource");
File[] files = dir.listFiles();
for(File f:files) {
//取文件名
String fileName = f.getName();
//文件的路径
String filePath = f.getPath();
//文件的内容
String fileContent = FileUtils.readFileToString(f, "utf-8");
//文件的大小
long fileSize = FileUtils.sizeOf(f);
//创建field
//参数1:域的名称 参数2:域的内容 参数3:是否存储
Field fieldName = new TextField("name", fileName, Field.Store.YES);
//Field fieldPath = new TextField("path", filePath, Field.Store.YES);
StoredField fieldPath = new StoredField("path",filePath);
Field fieldContent = new TextField("content", fileContent, Field.Store.YES);
//Field fieldSize = new TextField("size", fileSize + "", Field.Store.YES);
LongPoint fieldSizeValue = new LongPoint("size",fileSize);
Field fieldSizeStore = new StoredField("size",fileSize);
//创建文档对象
Document document = new Document();
//向文档对象中添加域
document.add(fieldName);
document.add(fieldPath);
document.add(fieldContent);
//document.add(fieldSize);
document.add(fieldSizeValue);
document.add(fieldSizeStore);
//5.把文档对象写入索引库
indexWriter.addDocument(document);
}
//6.关闭IndexWriter对象
indexWriter.close();
}
创建索引源码
猜你喜欢
转载自blog.csdn.net/wait_13/article/details/104215927
今日推荐
周排行