lucene-- 更新updateDocument

2014-11-24 09:06:59 · 作者: · 浏览: 0

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
writer.updateDocument(new Term("id","1"), doc); } 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
updateDocument(term, doc, getAnalyzer()); }

01
public void updateDocument(Term term, Document doc, Analyzer analyzer)
02
throws CorruptIndexException, IOException
03
{
04
ensureOpen();
05
try {
06
boolean doFlush = false;
07
boolean success = false;
08
try {
09
doFlush = this.docWriter.updateDocument(doc, analyzer, term); success = true;www.2cto.com
10
} finally {
11
。。。。
12
}

你看一下lucene的源码你会惊奇的发现

1
boolean addDocument(Document doc, Analyzer analyzer) throws CorruptIndexException, IOException {
2

3
return updateDocument(doc, analyzer, null); }
addDocument尽然调用的和update调用的同一个方法,对于上面的的解释明白了吧。


作者:bugeasy