lucene-- 更新updateDocument

Lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集

 先删除之后再添加

public void update() {
		IndexWriter writer = null;
		try {
			writer = new IndexWriter(directory,
					new IndexWriterConfig(Version.LUCENE_35,new StandardAnalyzer(Version.LUCENE_35)));
			/*
			 * Lucene并没有提供更新,这里的更新操作其实是如下两个操作的合集
			 * 先删除之后再添加
			 */
			Document doc = new Document();
			doc.add(new Field("id","11",Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
			doc.add(new Field("email",emails[0],Field.Store.YES,Field.Index.NOT_ANALYZED));
			doc.add(new Field("content",contents[0],Field.Store.NO,Field.Index.ANALYZED));
			doc.add(new Field("name",names[0],Field.Store.YES,Field.Index.NOT_ANALYZED_NO_NORMS));
			writer.updateDocument(new Term("id","1"), doc);  		} catch (CorruptIndexException e) {
			e.printStackTrace();
		} catch (LockObtainFailedException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(writer!=null) writer.close();
			} catch (CorruptIndexException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

 

 public void updateDocument(Term term, Document doc)
     throws CorruptIndexException, IOException
  {
    ensureOpen();
    updateDocument(term, doc, getAnalyzer());    }

 

public void updateDocument(Term term, Document doc, Analyzer analyzer)
   throws CorruptIndexException, IOException
  {
   ensureOpen();
    try {
      boolean doFlush = false;
     boolean success = false;
      try {
     doFlush = this.docWriter.updateDocument(doc, analyzer, term);        success = true;
     } finally {
。。。。
}

 

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

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

      return updateDocument(doc, analyzer, null);   }

 addDocument尽然调用的和update调用的同一个方法,对于上面的的解释明白了吧。

 

 

 

 

你可能感兴趣的:(Lucene,updateDocument)