【问题标题】:How to Synchronize RAMDirectory with FSDirectory in Apache Lucene 6.5如何在 Apache Lucene 6.5 中同步 RAMDirectory 和 FSDirectory
【发布时间】:2018-04-07 12:44:19
【问题描述】:

我的问题起草得很糟糕,我正在尝试将 RAMDirectory 索引备份到文件系统目录路径中,以便在发生任何崩溃时恢复索引。

这些方法我都试过了

 Directory.copy(ramDir, FSDirectory.open(indexDir), false);

但这种方法甚至没有在较新版本的 Lucene 中显示。

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

org.apache.lucene.index.IndexNotFoundException: no segments* file found in MMapDirectory

这是源代码

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 添加到文件系统。但没有任何效果。是我调用提交然后关闭的顺序这是错误的吗? RAMDirectory 有一个方法 Sync(Collection) ,它的 javadoc 正是我需要的,但我不知道如何使用它。 解决这个问题的最佳方法是什么。 以下答案我检查了所以但没有任何效果.. Directory.copy approach

【问题讨论】:

    标签: java exception lucene ramdirectory


    【解决方案1】:

    在我的案例中有效的方法是 我通过重新初始化并将其指向我希望它备份 RAMIndex 的 FSDirectory 来使用 indexWriter 的相同实例。 对 RAMDirectory 和 FSDirectory 使用相同的 Analyzer 实例。 为 SYNC up 任务定义 IWC(index writer config) 的单独实例。 代码如下

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-22
      • 2016-06-25
      • 1970-01-01
      • 1970-01-01
      • 2014-10-07
      • 2013-07-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多