首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ApacheLucene6.5中RAMDirectory与FSDirectory的同步

ApacheLucene6.5中RAMDirectory与FSDirectory的同步
EN

Stack Overflow用户
提问于 2018-04-07 12:44:19
回答 1查看 353关注 0票数 2

我的问题起草得不好,我正在尝试将RAMDirectory索引备份到FileSystem目录路径中,以便在发生崩溃时恢复索引。

我尝试过这些方法

代码语言:javascript
复制
 Directory.copy(ramDir, FSDirectory.open(indexDir), false);

但是这种方法甚至在Lucene的新版本中都没有显示出来。

我使用的第二种方法是indexwriter.addIndexes(),但是它正在抛出这个异常

org.apache.lucene.index.IndexNotFoundException:没有片段*文件在MMapDirectory中找到

这是源代码

代码语言:javascript
复制
BufferedReader reader=new BufferedReader(new FileReader("hash.txt"));
 RAMDirectory idx=new RAMDirectory();
    String str=reader.readLine();
    while(str!=null)
    {
        Document doc = new Document();
        doc.add(new StringField("SPAM",str, Field.Store.YES));  
        str=reader.readLine();
        dcmnts.add(doc);
    }
    String indexDir="C:\\Users\\xyz\\Desktop\\cmengine\\src\\com\\company\\lucene";
    Directory dir = FSDirectory.open(Paths.get(indexDir));
    writer.addDocuments(dcmnts);//here dcmnts is ArrayList<Documents>
    writer.commit();
    //  writer.addIndexes(dir); i even tried this didnt worked so i took 
    //seprate index writer 
    writer.close();
    IndexWriterConfig iwc2 = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);

    IndexWriter writer2=new IndexWriter(idx, iwc2);
    writer2.addIndexes(dir);

在这里,我甚至尝试使用相同的IndexWriter添加RAMDirectory来文件system.but,但没有任何效果。这是我叫提交的顺序吗?这是错的吗?RAMDirectory有一个方法Sync(Collection),它的javadoc正是我所需要的,但我不知道如何使用它。解决这个问题的最佳方法是什么?在回答之后,我查过了,但什么也没做。Directory.copy approach

EN

回答 1

Stack Overflow用户

发布于 2018-04-09 04:43:26

在我的例子中起作用的方法是,通过重新初始化indexWriter并将其指向希望它对RAMIndex进行备份的FSDirectory,从而使用相同的RAMIndex实例。对RAMDirectory和FSDirectory使用相同的分析器实例。为同步向上任务定义IWC(索引写入器配置)的单独实例。代码如下

代码语言:javascript
复制
    IndexWriterConfig iwc2 = new IndexWriterConfig(analyzer);
    iwc.setOpenMode(OpenMode.CREATE_OR_APPEND);
    Directory dir = FSDirectory.open(Paths.get(indexDir));
    writer=new IndexWriter(dir,iwc2);
    writer.addIndexes(idx);
    writer.close();
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49707507

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档