`
ttitfly
  • 浏览: 615941 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

通过updateDocument更新索引

阅读更多
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();
		}
	}

}

分享到:
评论
3 楼 TonyLian 2010-02-02  
有没有发现:更新后,下次再打开索引。
indexReader.maxDoc()的数量翻番了。
而且索引目录所占用的磁盘空间也翻番了(旧的索引文件还在,新的索引文件大小和旧的一样大)

只有在write.close();
前写 writer.optimize();
才可以避免此问题。

但是,writer.optimize();是一个很耗时、耗资源的动作。单单空白磁盘空间的需求就只是要有2倍于翻了翻后的索引文件的大小。

如果经常要updateDocument的话,每次都writer.optimize();会大大影响性能,不知道有没有两全其美的好方法?
2 楼 hqman 2008-09-21  
package com.wangkai.lucene;

import java.io.IOException;

import junit.framework.TestCase;

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 UpdateDocumentTests extends TestCase {

private static String path = "/home/hqman/shell/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.addDocument( docNew);

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 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;
}
System.out.println(" 搜索到:" + hits.length() + "'");

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();
}
}


public void testUpdate(){
addIndex();
updateIndex();
search("张三");

}
}


测试了 结果是 2条

搜索到:2'
id = 0
own id = 123456
userName = 张三
come from  = 北京

id = 1
own id = 123456
userName = 张三
come from  = 北京

1 楼 hqman 2008-09-21  
奇怪 我直接 writer.addDocument(doc); 也可以完成 更新索引

相关推荐

    Apache Lucene全文检索和IKAnalyzer分词工具类

    indexWriter.updateDocument(term, doc); /**optimize()方法是对索引进行优化 **/ indexWriter.optimize(); indexWriter.close(); } /** * 创建索引(多个) * @param list * @throws Exception ...

    Logical-escaped_fragment:此代码可帮助您处理 SEO (escaped_fragment) 和 AJAX 内容

    通过使用这两个功能,您的网站可以完全使用 AJAX,但 Google 会看到正确索引页面所需的内容。 阅读下面的解释细节以获取更多信息基于: : 基于:Google 执行$(document).ready()或任何等效的 DOM 就绪加载器的事实...

    Visual C++ 数据库系统开发完全手册.part2

    12.4.3 在UPDATE更新命令中使用子查询 12.5 删除命令DELETE 12.5.1 DELETE命令简介 12.5.2 简单的DELETE命令 12.5.3 在删除中使用子查询 12.6 数据备份与数据还原 12.6.1 数据备份命令BACKUP 12.6.2 数据还原命令...

    Visual C++ 数据库系统开发完全手册.part1

    12.4.3 在UPDATE更新命令中使用子查询 12.5 删除命令DELETE 12.5.1 DELETE命令简介 12.5.2 简单的DELETE命令 12.5.3 在删除中使用子查询 12.6 数据备份与数据还原 12.6.1 数据备份命令BACKUP 12.6.2 数据还原命令...

    基于J2EE框架的个人博客系统项目毕业设计论文(源码和论文)

    通过软件的修补、替换完成系统的升级和更新换代。 3、 系统的易用性和易维护性:要实现这一点,就要求系统应该尽量使用用户熟悉的术语和中文信息的界面;针对用户可能出现的使用问题,要提供足够的在线帮助,缩短...

    ASP.NET3.5从入门到精通

    9.3.1 SQL UPDATE 数据更新语句 9.3.2 使用Command 对象更新记录 9.3.3 使用DataSet 数据集更新记录 9.4 ASP.NET 删除数据 9.4.1 SQL DELETE 数据删除语句 9.4.2 使用Command 对象删除记录 9.4.3 使用DataSet 数据集...

    ASP.NET 3.5 开发大全11-15

    9.3.1 SQL UPDATE数据更新语句 9.3.2 使用Command对象更新记录 9.3.3 使用DataSet数据集更新记录 9.4 ASP.NET删除数据 9.4.1 SQL DELETE数据删除语句 9.4.2 使用Command对象删除记录 9.4.3 使用DataSet数据集删除...

    ASP.NET 3.5 开发大全

    9.3.1 SQL UPDATE数据更新语句 9.3.2 使用Command对象更新记录 9.3.3 使用DataSet数据集更新记录 9.4 ASP.NET删除数据 9.4.1 SQL DELETE数据删除语句 9.4.2 使用Command对象删除记录 9.4.3 使用DataSet数据集删除...

    ASP.NET 3.5 开发大全1-5

    9.3.1 SQL UPDATE数据更新语句 9.3.2 使用Command对象更新记录 9.3.3 使用DataSet数据集更新记录 9.4 ASP.NET删除数据 9.4.1 SQL DELETE数据删除语句 9.4.2 使用Command对象删除记录 9.4.3 使用DataSet数据集删除...

    ASP.NET 3.5 开发大全word课件

    9.3.1 SQL UPDATE数据更新语句 9.3.2 使用Command对象更新记录 9.3.3 使用DataSet数据集更新记录 9.4 ASP.NET删除数据 9.4.1 SQL DELETE数据删除语句 9.4.2 使用Command对象删除记录 9.4.3 使用DataSet数据集删除...

    ASPNET35开发大全第一章

    9.3.1 SQL UPDATE数据更新语句 9.3.2 使用Command对象更新记录 9.3.3 使用DataSet数据集更新记录 9.4 ASP.NET删除数据 9.4.1 SQL DELETE数据删除语句 9.4.2 使用Command对象删除记录 9.4.3 使用DataSet数据集删除...

    C#全能速查宝典

    1.4.29 LastIndexOf方法——确定字符在字符串中最后索引 70 1.4.30 Matches方法——检查字符串是否有重复的词出现 71 1.4.31 MONTH函数——返回指定日期中月部分的整数 73 1.4.32 PadLeft方法——在左边用空格填充 ...

    asp.net知识库

    通过反射调用類的方法,屬性,字段,索引器(2種方法) ASP.NET: State Server Gems 完整的动态加载/卸载程序集的解决方案 从NUnit中理解.NET自定义属性的应用(转载) 如何在.NET中实现脚本引擎 (CodeDom篇) .NET的插件...

    ZendFramework中文文档

    7.13.1. 从 1.0.x 到 1.5.0 或更新的版本的移植 7.13.2. 从 0.9.3 到 1.0.0RC1 或更新的版本的移植 7.13.3. 从 0.9.2 移植到 0.9.3 或更新的版本 7.13.4. 从 0.6.0 移植到 0.8.0 或更新的版本 7.13.5. 从 0.2.0 ...

    oracle动态性能表

     按照OracleDocument中的描述,v$sysstat存储自数据库实例运行那刻起就开始累计全实例(instance-wide)的资源使用情况。 类似于v$sesstat,该视图存储下列的统计信息: 1&gt;.事件发生次数的统计(如:user commits) 2&gt;...

Global site tag (gtag.js) - Google Analytics