1.1.1
实现步骤
第一步:创建一个IndexWriter
对象打开索引库
第二步:创建一个Document
对象。向document
中添加域。
第三步:把document
写入索引库。
第四步:关闭索引库。
1.1.2 代码实现
// 添加文档
@Test
public void addDocument() throws Exception {
// 打开索引库
// 指定索引库存放的位置
Directory directory = FSDirectory
.open(new File("D:\\temp\\0315\\index"));
// 创建一个分析器对象
Analyzer analyzer = new IKAnalyzer();
// 创建一个IndexWriterConfig 对象
IndexWriterConfig config = new IndexWriterConfig(Version.LATEST,
analyzer);
// 创建indexwriter对象
IndexWriter indexWriter = new IndexWriter(directory, config);
// 创建一个文档对象
Document document = new Document();
Field nameField = new TextField("name", "新添加的文档", Store.YES);
Field contentField = new TextField(
"content",
"中日韩统一表意文字(CJK Unified Ideographs),目的是要把分别来自中文、日文、韩文、越文中,本质、意义相同、形状一样或稍异的表意文字(主要为汉字,但也有仿汉字如日本国字、韩国独有汉字、越南的喃字)于ISO 10646及Unicode标准内赋予相同编码。",
Store.YES);
document.add(nameField);
document.add(contentField);
//写入索引库
indexWriter.addDocument(document);
//关闭indexwriter
indexWriter.close();
}
这里我们会发现, 我们之前添加的不会被覆盖或者删除,而是追加,在之前的域进行追加,且域是求的并集。
2.1.1
根据查询删除
查询到多个文档就删除多少。
public class deleteIndexFiles {
//根据查询删除文档
@Test
public void deleteDocumentByQuery() throws Exception {
// 打开索引库
// 指定索引库存放的位置
Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
// 创建一个分析器对象
Analyzer analyzer = new MyWordAnalyzer();
// 创建一个IndexWriterConfig 对象
IndexWriterConfig config = new IndexWriterConfig(analyzer);// 创建indexwriter对象
IndexWriter indexWriter = new IndexWriter(directory, config);
// 创建一个文档对象
//创建一个Query对象
Query query = new TermQuery(new Term("name", "apache"));
indexWriter.deleteDocuments(query);
//提交修改
indexWriter.commit();
indexWriter.close();
}
}
2.1.2
全部删除 该功能慎用
1、
删除索引库。
2、
使用indexwriter
的deleteAll
方法。
//删除全部文档
@Test
public void deleteAllDocument() throws Exception {
// 打开索引库
// 指定索引库存放的位置
Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
// 创建一个分析器对象
Analyzer analyzer = new MyWordAnalyzer();
// 创建一个IndexWriterConfig 对象
IndexWriterConfig config = new IndexWriterConfig(analyzer);// 创建indexwriter对象
IndexWriter indexWriter = new IndexWriter(directory, config);
indexWriter.deleteAll();
indexWriter.close();
}
3.1.1
修改索引
其实更新的本质是先删除再追加,效果看起来像是修改了索引
//更新文档
@Test
public void updateDocument() throws Exception {
// 打开索引库
// 指定索引库存放的位置
Directory directory = FSDirectory.open(Paths.get("D:\\LucentTest\\luceneIndex"));
// 创建一个分析器对象
Analyzer analyzer = new MyWordAnalyzer();
// 创建一个IndexWriterConfig 对象
IndexWriterConfig config = new IndexWriterConfig(analyzer);// 创建indexwriter对象
IndexWriter indexWriter = new IndexWriter(directory, config);
//创建一个文档对象
Document doc = new Document();
doc.add(new TextField("name", "更新后的文档", Field.Store.YES));
doc.add(new TextField("content", "更新后的文档内容", Field.Store.YES));
doc.add(new TextField("address", "北京昌平", Field.Store.YES));
//第一个参数term对象,指定要查询的域和关键词
//第二个参数,要更新的文档。
indexWriter.updateDocument(new Term("name", "apache"), doc);
//关闭
indexWriter.close();
}
注:这里有几点注意,我们在做分词的过程要将分析器保持一致,如在之前的例子中用的是标准的分析器,但在上述所写的例子中 用的都是 自定义的分析器,很可能会导致查询不出来或报错的信息,这里一定要谨慎使用。
下面是小编的微信转帐二维码,小编再次谢谢读者的支持,小编会更努力的
----请看下方↓↓↓↓↓↓↓
百度搜索 Drools从入门到精通:可下载开源全套Drools教程
深度Drools教程不段更新中:
更多Drools实战陆续发布中………
扫描下方二维码关注公众号 ↓↓↓↓↓↓↓↓↓↓