wqkasdf1224
2011-08-22 11:52
浏览 469

lucene更新索引,被更新索引的还在,还能被搜索到

使用lucene进行索引更新时,被更新的那条索引还存在;如果单独调用删除索引的方法的话,那条索引可以被删除

代码如下:
[color=red]// 生成索引[/color]
public boolean saveIndex(String id, String userId, String url, String title, String contents, String author, String date) {
File indexFile = new File(indexDirPath);
indexFile.mkdirs();
if(!indexFile.exists()) {
try {
indexFile.createNewFile();
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
File[] files = new File(indexDirPath).listFiles();
boolean isCreate = true; //判断索引文件的建立是新建还是追加模式
if(files.length>0) {
isCreate = false;
}
/*超链接,主要应用于附件的使用,比如附件下载 每个元素的都是附件路径和附件名以逗号隔开的字符串*/
String accessStr = this.getUrlStr(id.trim());

    IndexWriter indexWriter = null;
    Directory directory = null;
    try {
        directory = new SimpleFSDirectory(indexFile);           
        //索引配置
        IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_30, analyzer);
        //创建索引模式:CREATE,覆盖模式; APPEND,追加模式
        if(isCreate) {
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        } else {
            indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.APPEND);
        }

// indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
//初始化索引创建器
indexWriter = new IndexWriter(directory,indexWriterConfig);
Document doc = new Document();
// 创建Field对象,并放入doc对象中
doc.add(new Field("id", id.trim(), Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("userId", userId, Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("url", url, Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("title", title, Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("contents", contents, Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("accesses", accessStr, Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("author", author, Field.Store.YES,
Field.Index.ANALYZED));
doc.add(new Field("date", date, Field.Store.YES,
Field.Index.NOT_ANALYZED));
doc.add(new Field("indexDate", DateTools.dateToString(new Date(),
DateTools.Resolution.DAY), Field.Store.YES,
Field.Index.NOT_ANALYZED));
// 写入IndexWriter
indexWriter.addDocument(doc);
// 查看IndexWriter里面有多少个索引
// System.out.println("numDocs" + indexWriter.numDocs());
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

     [color=red]// 删除索引[/color]
     public boolean deleteIndex(String id) { 
    /*File classDir = new File(this.getClass().getClassLoader().getResource("").getPath());
    String proDir = classDir.getParentFile().getParentFile().getPath();*/
    File indexFile = new File(indexDirPath);

// File indexDir = new File("D:\temp\indexDir");
Directory directory;
try {
directory = new SimpleFSDirectory(indexFile);
/*IndexReader ir = IndexReader.open(directory,false); //true:索引打开模式为只读(readOnly);
// false:索引打开模式不为可读,即为可更改
Term term = new Term("id",id);
ir.deleteDocuments(term);
ir.close();*/
IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_30, analyzer);
IndexWriter iw = new IndexWriter(directory,indexWriterConfig);
Term term = new Term("id",id);
iw.deleteDocuments(term);
iw.optimize();
iw.commit();
iw.close();

    } catch (IOException e) {
        e.printStackTrace();
        return false;
    }
    return true;
}

    [color=red]// 更新索引的方法[/color]
public boolean updateIndex(String id, String userId, String url, String title, String contents, String author,
                 String date) {
    /*this.deleteIndex(id.trim());
    if(this.deleteIndex(id.trim())) {
        this.saveIndex(id.trim(), userId, url, title, contents, author, date);
    }*/
    File indexFile = new File(indexDirPath);
         // 超链接,主要应用于附件的使用,比如附件下载   每个元素的都是附件路径和附件名以逗号隔开的字符串
    String accessStr = this.getUrlStr(id);
    Term term = new Term("id",id);
    IndexWriter indexWriter = null;
    Directory directory = null;
    try {
        directory = new SimpleFSDirectory(indexFile);
        //索引配置
            // IndexWriterConfig indexWriterConfig=new IndexWriterConfig(Version.LUCENE_30, analyzer);
        //创建索引模式:CREATE,覆盖模式; APPEND,追加模式

                   //   indexWriterConfig.setOpenMode(IndexWriterConfig.OpenMode.CREATE);
        //初始化索引创建器
        indexWriter = new IndexWriter(directory, analyzer, false, 
                                  IndexWriter.MaxFieldLength.UNLIMITED);
        indexWriter.setUseCompoundFile(true);
        Document doc = new Document();
        // 创建Field对象,并放入doc对象中
        doc.add(new Field("id", id, Field.Store.YES,
                Field.Index.NOT_ANALYZED));
        doc.add(new Field("userId", userId, Field.Store.YES,
                Field.Index.NOT_ANALYZED));
        doc.add(new Field("url", url, Field.Store.YES,
                Field.Index.ANALYZED));
        doc.add(new Field("title", title, Field.Store.YES,
                Field.Index.ANALYZED));
        doc.add(new Field("contents", contents, Field.Store.YES,
                Field.Index.ANALYZED));
        doc.add(new Field("accesses", accessStr, Field.Store.YES, 
                Field.Index.ANALYZED));
        doc.add(new Field("author", author, Field.Store.YES,
                Field.Index.ANALYZED));
        doc.add(new Field("date", date, Field.Store.YES,
                Field.Index.NOT_ANALYZED));
        doc.add(new Field("indexDate", DateTools.dateToString(new Date(),
                DateTools.Resolution.DAY), Field.Store.YES,
                Field.Index.NOT_ANALYZED));
        // 更新IndexWriter
        indexWriter.updateDocument(term, doc);
        // 查看IndexWriter里面有多少个索引

// System.out.println("numDocs" + indexWriter.numDocs());
indexWriter.commit();
indexWriter.close();
} catch (IOException e) {
e.printStackTrace();
return false;
}
return true;
}

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • caizi1288 2011-08-25 14:44
    已采纳

    删除的还在,那是你根本没删除掉。删除数据时候一定要保持数据库,缓存和索引都删除干净。

    已采纳该答案
    打赏 评论
  • caizi1288 2011-08-24 16:27

    更新索引时候,你可以把之前的索引删除掉,再重新插入一边不就OK了。

    打赏 评论
  • AngelAndAngel 2011-08-25 13:49

    把缓存清掉

    打赏 评论
  • AngelAndAngel 2011-08-25 13:57

    你的索引是不是被锁住了啊

    打赏 评论
  • caizi1288 2011-08-25 14:46

    lukeall.jar 这是个查看索引里面数据的一个小工具,你网上下载下来看有帮助吗。

    打赏 评论

相关推荐 更多相似问题