---
title: "通过updateDocument更新索引"
author: "ttitfly"
date: "2008-09-20T15:57:30Z"
tags: []
slug: "updatedocument"
draft: false
lastmod: "2008-09-20T15:57:30Z"
id: 244935
url: "https://ttitfly.iteye.com/blog/244935"
---

```java

package com.lucene;

import java.io.IOException;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;

public class UpdateDocument {
	
	private static String path = "d:/index";
	
	
	public static void main(String[] args){
//		addIndex();
		updateIndex();
		search("李四");
		search("王五");
	}
	
	public static void addIndex(){
		try {
			IndexWriter write = new IndexWriter(path,new StandardAnalyzer(),true);
			
			Document doc = new Document();
			doc.add(new Field("id","123456",Field.Store.YES,Field.Index.UN_TOKENIZED));
			doc.add(new Field("userName","张三",Field.Store.YES,Field.Index.TOKENIZED));
			doc.add(new Field("comefrom","北京",Field.Store.YES,Field.Index.TOKENIZED));
			
			write.addDocument(doc);
			
			write.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	public static void updateIndex(){
		try {
			
			IndexWriter write = new IndexWriter(path,new StandardAnalyzer(),false);
			Document docNew = new Document();
			docNew.add(new Field("id","123456",Field.Store.YES,Field.Index.UN_TOKENIZED));
			docNew.add(new Field("userName","王五",Field.Store.YES,Field.Index.TOKENIZED));
			Term term = new Term("id","123456");
			/**
			  调用updateDocument的方法，传给它一个新的doc来更新数据，
			  Term term = new Term("id","1234567");
			  先去索引文件里查找id为1234567的Doc,如果有就更新它(如果有多条，最后更新后只有一条)。如果没有就新增.
			 
			  数据库更新的时候，我们可以只针对某个列来更新，而lucene只能针对一行数据更新。
			 */
			write.updateDocument(term, docNew);
			
			write.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Query queryParser(String str){
		QueryParser queryParser = new QueryParser("userName", new StandardAnalyzer());
		try {
			Query query =  queryParser.parse(str);
			return query;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static void search(String str){
		try {
			IndexSearcher search = new IndexSearcher(path);
			
			Query query = queryParser(str);
			
			Hits hits = search.search(query);
			if(hits==null){
				return;
			}
			if(hits.length() == 0){
				System.out.println(" 没有搜索到'" + str+"'");
				return;
			}
			for (int i = 0; i < hits.length(); i++) {
				Document doc = hits.doc(i);
				System.out.println("id = "+hits.id(i));
				System.out.println("own id = " + doc.get("id"));
				System.out.println("userName = "+doc.get("userName"));
				System.out.println("come from  = "+doc.get("comefrom"));
				System.out.println("");
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}


```