Lucene -- Update updateDocument

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.