Lucene does not provide updates. The update operations here are actually a collection of the following two operations:
Delete and then add
01
Public void update (){
02
IndexWriter writer = null;
03
Try {
04
Writer = new IndexWriter (directory,
05
New IndexWriterConfig (Version. inclue_35, new StandardAnalyzer (Version. inclue_35 )));
06
/*
07
* Lucene does not provide updates. The update operations here are actually a collection of the following two operations:
08
* Delete and then add
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 analyzer)
02
Throws CorruptIndexException, IOException
03
{
04
EnsureOpen ();
05
Try {
06
Boolean doFlush = false;
07
Boolean success = false;
08
Try {
09
DoFlush = this.doc Writer. <span style = "color: #003399;"> updateDocument (doc, analyzer, term); </span> success = true; www.2cto.com
10
} Finally {
11
....
12
}
You may be surprised to find out the lucene source code.
1
Boolean addDocument (Document doc, Analyzer analyzer) throws CorruptIndexException, IOException {
2
3
Return <span style = "color: #003399;"> updateDocument (doc, analyzer, null); </span>}
AddDocument calls the same method as update. You can understand the above explanation.
Author: bugeasy