索引的增删改查,因为查询有几种情况,不想代码太长,就将查分离出来了,下一篇写查
//创建索引
@Test
public void creatIndex() throws IOException{
Directory directory = FSDirectory.open(new File("G:\\a\\v"));
Analyzer ikAnalyzer = new IKAnalyzer();
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, ikAnalyzer);
//获得indexwriter
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//获得资源文件集合
File f = new File("G:\\a\\新建文件夹");
File[] listFiles = f.listFiles();
for (File file : listFiles) {
//创建文档对象
Document document = new Document();
//获得每个文件的属性:
String file_name = file.getName();
long file_size = FileUtils.sizeOf(file);
String file_path = file.getPath();
String file_content = FileUtils.readFileToString(file);
//创建四个域,分别存放着四个信息
TextField filename = new TextField("file_name", file_name,Store.YES);
TextField filesize = new TextField("file_size", String.valueOf(file_size),Store.YES);
TextField filepath = new TextField("file_path", file_path,Store.YES);
TextField filecontent = new TextField("file_content", file_content,Store.YES);
document.add(filename);
document.add(filesize);
document.add(filepath);
document.add(filecontent);
indexWriter.addDocument(document);
}
indexWriter.close();
}
// 查询索引
@Test
public void testSearchIndex() throws Exception {
// 第一步:查询准备工作
// 创建Directory对象
Directory dir = FSDirectory.open(new File("G:\\a\\v"));
// 创建IndexReader对象
IndexReader reader = DirectoryReader.open(dir);
// 创建IndexSearcher对象
IndexSearcher searcher = new IndexSearcher(reader);
// 第二步:创建查询条件对象
TermQuery query = new TermQuery(new Term("file_name", "全文"));
// 第三步:执行查询(参数1:查询条件对象,参数2:查询结果返回的最大值)
TopDocs topDocs = searcher.search(query, 10);
// 第四步:处理查询结果
// 输出结果数量
System.out.println("查询的结果数量:" + topDocs.totalHits);
// 取得结果集
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
// 遍历结果集
for (ScoreDoc scoreDoc : scoreDocs) {
// 根据文档对象ID取得文档对象
Document doc = searcher.doc(scoreDoc.doc);
// 打印搜索结果内容
// 文件名称
System.out.println("文件名称:" + doc.get("file_name"));
// 文件路径
System.out.println("文件路径:" + doc.get("file_path"));
// 文件大小
System.out.println("文件大小:" + doc.get("file_size"));
}
// 关闭IndexReader对象
reader.close();
}
//删除索引——全部删除
@Test
public void deleteAll() throws IOException{
//获得索引存放位置的
Directory directory = FSDirectory.open(new File("G:\\a\\v"));
//获得分词器
Analyzer analyzer=new IKAnalyzer();
//获得indexwriterconfig 对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
//获得indexwriter对象
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//====================
indexWriter.deleteAll();
//=====================
indexWriter.close();
}
//删除索引——按条件删除
@Test
public void delete() throws IOException{
//获得directory对象
Directory directory = FSDirectory.open(new File("G:\\a\\v"));
//获得分词器
Analyzer analyzer =new IKAnalyzer();
//获得indexwriterconfig 对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3,analyzer);
//获得indexwriter 对象
IndexWriter indexWriter = new IndexWriter(directory, indexWriterConfig);
//===========================================
Term t = new Term("fileName","全文");
TermQuery query = new TermQuery(t);
indexWriter.deleteDocuments(query);
//=============================================
indexWriter.close();
}
//更新索引
@Test
public void update() throws IOException{
//获得directory对象
Directory open = FSDirectory.open(new File("G:\\a\\v"));
//获得分词器
Analyzer analyzer=new IKAnalyzer();
//获得indexwriterconfig 对象
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_3, analyzer);
//获得indexwriter对象
IndexWriter indexWriter = new IndexWriter(open,indexWriterConfig);
//更新索引
//=================================================
Document doc = new Document();
doc.add(new TextField("filen", "测试文件名",Store.YES));
doc.add(new TextField("fileC", "测试文件内容",Store.YES));
indexWriter.updateDocument(new Term("file_name","全文"), doc, new IKAnalyzer());
//===============================================
indexWriter.close();
}