增删改查操作
public class IndexEdit {
private String indexDir;
public static String FIELD_NAME = "name";
public static String FIELD_CONTEXT = "context";
private String[] contexts = {
"this is beijing, it is a beautiful city",
"this is nanjing, it is culture city",
"this is shanghai, it is a business city"
};
private String[] names = {
"beijing",
"nanjing",
"shanghai"
};
public IndexEdit(String indexDir) {
this.indexDir = indexDir;
}
/**
* 添加文档
* @throws IOException
*/
public void addDocument() throws IOException {
IndexWriter iw = this.getWriter(indexDir);
for(int i=0; i<contexts.length; i++) {
Document doc = new Document();
doc.add(new TextField(FIELD_NAME, names[i], Field.Store.YES));
doc.add(new TextField(FIELD_CONTEXT, contexts[i], Field.Store.YES));
iw.addDocument(doc);
}
System.out.println("写入完毕,当前文档数:"+iw.numDocs());
System.out.println("---------------------------------------------------");
iw.close();
}
/**
* s删除文档,合并前
* @throws IOException
*/
public void testDeleteBeforeMerge() throws IOException {
IndexWriter iw = this.getWriter(indexDir);
System.out.println("删除文档,合并前,开始之前");
System.out.println("最大文档数:"+iw.maxDoc());
System.out.println("实际文档数:"+iw.numDocs());
Term term = new Term(FIELD_NAME, names[0]);
//此时删除只是改了状态,并没有真的删除,所以实际文档数要小于最大文档数
iw.deleteDocuments(term);
iw.commit();
System.out.println("删除文档,合并前,开始之后");
System.out.println("最大文档数:"+iw.maxDoc());
System.out.println("实际文档数:"+iw.numDocs());
System.out.println("---------------------------------------------------");
iw.close();
}
/**
* 删除文档,合并后
* @throws IOException
*/
public void testDeleteAfterMerge() throws IOException {
IndexWriter iw = this.getWriter(indexDir);
System.out.println("删除文档,合并后,开始之前");
System.out.println("最大文档数:"+iw.maxDoc());
System.out.println("实际文档数:"+iw.numDocs());
Term term = new Term(FIELD_NAME, names[1]);
iw.deleteDocuments(term);
//执行此方法之后,会真的删除,所以最大文档数会和实际文档数相同
iw.forceMergeDeletes();
iw.commit();
System.out.println("删除文档,合并后,开始之后");
System.out.println("最大文档数:"+iw.maxDoc());
System.out.println("实际文档数:"+iw.numDocs());
System.out.println("---------------------------------------------------");
iw.close();
}
/**
* 测试更新
* @throws IOException
*/
public void updateDoc() throws IOException {
IndexWriter iw = this.getWriter(indexDir);
Term term = new Term(FIELD_NAME, "beijing");
Document doc = new Document();
doc.add(new TextField(FIELD_NAME, "beijing", Field.Store.YES));
doc.add(new TextField(FIELD_CONTEXT, "there is beijing", Field.Store.YES));
//修改是把之前的删除了,把新的添加进去
iw.updateDocument(term , doc);
System.out.println("更新完毕,当前文档数:"+iw.numDocs());
System.out.println("---------------------------------------------------");
iw.commit();
iw.close();
}
/**
* 获取indexWirter
* @param indexDir
* @return
* @throws IOException
*/
private IndexWriter getWriter (String indexDir) throws IOException {
Directory dir = FSDirectory.open(Paths.get(indexDir));
Analyzer analyzer = new StandardAnalyzer();
IndexWriterConfig conf = new IndexWriterConfig(analyzer );
IndexWriter indexWriter = new IndexWriter(dir , conf);
return indexWriter;
}
}
测试
public class Main {
public static void main(String[] args) throws IOException, ParseException {
String indexDir = "E://Luence/01";
IndexEdit ie = new IndexEdit(indexDir);
//ie.addDocument();
ie.updateDoc();
//ie.testDeleteBeforeMerge();
ie.testDeleteAfterMerge();
search(indexDir, "city");
}
private static void search(String indexDir, String q) throws IOException, ParseException {
Directory dir = FSDirectory.open(Paths.get(indexDir));
IndexReader r = DirectoryReader.open(dir);
IndexSearcher is = new IndexSearcher(r);
Analyzer analyzer = new StandardAnalyzer();
QueryParser parser=new QueryParser(IndexEdit.FIELD_CONTEXT,analyzer);
Query query = parser.parse(q);
TopDocs top = is.search(query, 10);
System.out.println("结果数:"+top.totalHits);
for(ScoreDoc s : top.scoreDocs) {
Document doc = is.doc(s.doc);
System.out.println(doc.get(IndexEdit.FIELD_NAME));
System.out.println(doc.get(IndexEdit.FIELD_CONTEXT));
System.out.println("-------------------------------");
}
}
}