有用过lucene的吗??请问一下

yangxiao_jiang 2003-12-04 10:06:10
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;


import org.apache.lucene.analysis.SimpleAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

/*
* Created on 2003-12-3
*
* To change the template for this generated file go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/

/**
* @author Administrator
*
* To change the template for this generated type comment go to
* Window>Preferences>Java>Code Generation>Code and Comments
*/
public class IndexFiles {
//使用方法:: IndexFiles [索引输出目录] [索引的文件列表] ...
public static void main(String[] args) throws Exception {
String indexPath = args[0]; //索引输出目录 indexpath
IndexWriter writer;
//用指定的语言分析器构造一个新的写索引器(第3个参数表示是否为追加索引)
writer = new IndexWriter(indexPath, new SimpleAnalyzer(), false);
for (int i=1; i<args.length; i++)
{
System.out.println("Indexing file " + args[i]);
InputStream is = new FileInputStream(args[i]);
//构造包含2个字段Field的Document对象
//一个是路径path字段,不索引,只存储
//一个是内容body字段,进行全文索引,并存储
Document doc = new Document();
doc.add(Field.UnIndexed("path", args[i]));
doc.add(Field.Text("body", (Reader) new InputStreamReader(is)));
//将文档写入索引
writer.addDocument(doc);
is.close();
};
//关闭写索引器
writer.close();
}
}
显示的错误是
java.io.IOException: Index locked for write: Lock@C:\test\write.lock
at org.apache.lucene.index.IndexWriter.<init>(Unknown Source)
at org.apache.lucene.index.IndexWriter.<init>(Unknown Source)
at IndexFiles.main(IndexFiles.java:31)
Exception in thread "main"
应该怎么改??
...全文
41 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
pigo 2003-12-05
  • 打赏
  • 举报
回复
可能是少了:

writer.optimize();
writer.close();
因为没有对文件进行锁定。

Eclipse中文语言包下载:

http://download2.eclipse.org/downloads/drops/L-2.1.x%20Translations-200307021300/eclipse2.1.1-SDK-win-LanguagePackFeature.zip







下面是lucene自己带的demo:

class IndexFiles {
public static void main(String[] args) {
try {
long starttime = System.currentTimeMillis();

IndexWriter writer = new IndexWriter("index", new StandardAnalyzer(), true);
indexDocs(writer, new File(args[0]));
writer.optimize();
writer.close();
System.out.print(System.currentTimeMillis() - starttime);
System.out.println(" total milliseconds");

}
catch (Exception e) {
System.out.println(" caught a " + e.getClass() +
"\n with message: " + e.getMessage());
}
}

public static void indexDocs(IndexWriter writer, File file) throws Exception {
if (file.isDirectory()) {
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
indexDocs(writer, new File(file, files[i]));
}
}
else {
System.out.println("adding " + file);
writer.addDocument(FileDocument.Document(file));
}
}
}
yangxiao_jiang 2003-12-05
  • 打赏
  • 举报
回复
没有人会??

那有人知道
NLS-SDK-2.1.0-Translations.zip能在什么地方下载到吗 ??
是elipse2.1的多国语言支持

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧