我的问题起草得不好,我正在尝试将RAMDirectory索引备份到FileSystem目录路径中,以便在发生崩溃时恢复索引。
我尝试过这些方法
Directory.copy(ramDir, FSDirectory.open(indexDir), false);但是这种方法甚至在Lucene的新版本中都没有显示出来。
我使用的第二种方法是indexwriter.addIndexes(),但是它正在抛出这个异常
org.apache.lucene.index.IndexNotFoundException:没有片段*文件在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来文件system.but,但没有任何效果。这是我叫提交的顺序吗?这是错的吗?RAMDirectory有一个方法Sync(Collection),它的javadoc正是我所需要的,但我不知道如何使用它。解决这个问题的最佳方法是什么?在回答之后,我查过了,但什么也没做。Directory.copy approach
发布于 2018-04-09 04:43:26
在我的例子中起作用的方法是,通过重新初始化indexWriter并将其指向希望它对RAMIndex进行备份的FSDirectory,从而使用相同的RAMIndex实例。对RAMDirectory和FSDirectory使用相同的分析器实例。为同步向上任务定义IWC(索引写入器配置)的单独实例。代码如下
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();https://stackoverflow.com/questions/49707507
复制相似问题