在使用lucene创建索引库中,介绍了一些基础的lucene概念,如何用lucene进行文档添加呢。
private static void testAddDocument() throws Exception {
//创建一个IndexWriter对象,需要使用IKAnalyzer作为分析器
IndexWriter indexWriter = new IndexWriter(
FSDirectory.open(new File("J:\\storeSpace\\luceneStoreSpace").toPath()),
new IndexWriterConfig(new IKAnalyzer()));
//创建一个document对象
Document document = new Document();
//向document对象中添加域
Field name = new TextField("name","标题1", Field.Store.YES);
document.add(name);
Field content = new TextField("content","内容1", Field.Store.YES);
document.add(content);
Field path = new StoredField("path","path1");
document.add(path);
//把文档写入索引库
indexWriter.addDocument(document);
//关闭索引库
indexWriter.close();
}
这样就可以实现添加文档了。