lucene-- 更新updateDocument
Lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集
先删除之后再添加
01
public void update() {
02
IndexWriter writer = null;
03
try {
04
writer = new IndexWriter(directory,
05
new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
06
/*
07
* Lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集
08
* 先删除之后再添加
09
*/
10
Document doc = new Document();
11
doc.add(new Field("id","11",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
12
doc.add(new Field("email",emails[0],Field.Store.YES,Field.Index.NOT_ANALYZED));
13
doc.add(new Field("content",contents[0],Field.Store.NO,Field.Index.ANALYZED));
14
doc.add(new Field("name",names[0],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
15
<span style="color:#e53333;">writer.updateDocument(new Term("id","1"), doc); </span> } catch (CorruptIndexException e) {
16
e.printStackTrace();
17
} catch (LockObtainFailedException e) {
18
e.printStackTrace();
19
} catch (IOException e) {
20
e.printStackTrace();
21
} finally {
22
try {
23
if(writer!=null) writer.close();
24
} catch (CorruptIndexException e) {
25
e.printStackTrace();
26
} catch (IOException e) {
27
e.printStackTrace();
28
}
29
}
30
}
1
public void updateDocument(Term term, Document doc)
2
throws CorruptIndexException, IOException
3
{
4
ensureOpen();
5
<span style="color:#e53333;"> updateDocument(term, doc, getAnalyzer());</span> }
01
public void updateDocument(Term term, Document doc, Analyzer 易做图yzer)
02
throws CorruptIndexException, IOException
03
{
04
ensureOpen();
05
try {
06
boolean doFlush = false;
07
boolean success = false;
08
try {
09
doFlush = this.docWriter.<span style="color:#003399;">updateDocument(doc, 易做图yzer, term);</span> success = true;www.zzzyk.com
10
} finally {
11
。。。。
12
}
你看一下lucene的源码你会惊奇的发现
1
boolean addDocument(Document doc, Analyzer 易做图yzer) throws CorruptIndexException, IOException {
2
3
return <span style="color:#003399;">updateDocument(doc, 易做图yzer, null);</span> }
addDocument尽然调用的和update调用的同一个方法,对于上面的的解释明白了吧。
作者:bugeasy
补充:软件开发 , Java ,