luence2.2 无法删除指定索引

ya9534 2010-09-02 11:58:54
代码如下:
import java.io.IOException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.IndexWriter;

public class upIndex {
public void upIndex(){
try{
IndexReader ir = IndexReader.open("C:\\index");
System.out.println("索引中的文档数量:"+ir.numDocs());
int r = ir.deleteDocuments(new Term("filename", "j79-1008.txt"));
System.out.println("己删除"+r+"个索引");
ir.close();
ir=IndexReader.open("C:\\index");
System.out.println("删除文档后的数量"+ir.numDocs());


}catch (Exception e) {
System.out.println("出错");
}

}

public static void main(String[] args) {
upIndex ed = new upIndex();
ed.upIndex();
}
}

运行输出结果:
索引中的文档数量:2790
己删除0个索引
删除文档后的数量2790

哪里出问题了?大侠们请多多指点
...全文
58 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
vinna9534 2010-09-08
  • 打赏
  • 举报
回复
建立索引时自定义一个field段,给一个唯一值如:doc.add(new Field("title", String.valueOf(num), Field.Store.YES, Field.Index.TOKENIZED));
删除时使用int r = ir.deleteDocuments(new Term("title", "123"));
ya9534 2010-09-02
  • 打赏
  • 举报
回复
把这句int r = ir.deleteDocuments(new Term("filename", "j79-1008.txt"));改为如下
int r = ir.deleteDocuments(new Term("contents", "123"));执行成功
ya9534 2010-09-02
  • 打赏
  • 举报
回复
getDocument()应该是这样:

private Document getDocument(File f) throws Exception {
char[] charInData = new char[1000];
String str="";
int length = 0;
// 生成文档对象
Document doc = new Document();
// 获取文件流
FileInputStream is = new FileInputStream(f);
Reader reader = new BufferedReader(new InputStreamReader(is));
InputStreamReader inReader = new InputStreamReader(new FileInputStream(f.getCanonicalPath()), "utf-8");
while ((length = inReader.read(charInData)) != -1) {
str += String.valueOf(charInData, 0, length);
}

// 添加索引内容
doc.add(new Field("contents", str, Field.Store.YES,Field.Index.TOKENIZED));

doc.add(new Field("path",f.getCanonicalPath(), Field.Index.TOKENIZED));
System.out.println(f.getPath());
return doc;
}
ya9534 2010-09-02
  • 打赏
  • 举报
回复
此处贴上建立索引时的代码:
……
public void writeToIndex() throws Exception {
File folder = new File(Constants.INDEX_FILE_PATH);
if (folder.isDirectory()) {
String[] files = folder.list();
for (int i = 0; i < files.length; i++) {
File file = new File(folder, files[i]);
Document doc = getDocument(file);
System.out.println("正在建立索引" + file + "");
writer.addDocument(doc);
}
}
writer.optimize();
}

private Document getDocument(File f) throws Exception {
// 生成文档对象
Document doc = new Document();
// 获取文件流
FileInputStream is = new FileInputStream(f);
Reader reader = new BufferedReader(new InputStreamReader(is));
// 添加索引内容
doc.add(new Field("contents", new InputStreamReader(new FileInputStream(f.getCanonicalPath()), "utf-8")));
doc.add(new Field("path", f.getCanonicalPath(), Field.Store.YES,
Field.Index.NO));
return doc;
}
……

2,760

社区成员

发帖
与我相关
我的任务
社区描述
搜索引擎的服务器通过网络搜索软件或网络登录等方式,将Internet上大量网站的页面信息收集到本地,经过加工处理建立信息数据库和索引数据库。
社区管理员
  • 搜索引擎技术社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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