【问题标题】:How to write/serialize lucene's ByteBuffersDirectory to disk?如何将 lucene 的 ByteBuffersDirectory 写入/序列化到磁盘?
【发布时间】:2022-01-10 05:21:39
【问题描述】:

如何将 Lucene 8.11 ByteBuffersDirectory 写入磁盘?
类似于 Lucene 2.9.4 Directory.copy(directory, FSDirectory.open(indexPath), true)

【问题讨论】:

    标签: lucene


    【解决方案1】:

    您可以使用copyFrom 方法来执行此操作。

    例如:

    您使用的是ByteBuffersDirectory

    final Directory dir = new ByteBuffersDirectory();
    

    假设您没有同时向 dir 写入任何新数据,您可以声明要写入数据的目标 - 例如,FSDirectory(文件系统目录):

    Directory to = FSDirectory.open(Paths.get(OUT_DIR_PATH));
    

    使用您想要的任何字符串作为OUT_DIR_PATH 位置。

    然后您可以遍历原始dir 对象中的所有文件,将它们写入这个新的to 位置:

    IOContext ctx = new IOContext();
    for (String file : dir.listAll()) {
        System.out.println(file); // just for testing
        to.copyFrom(dir, file, file, ctx);
    }
    

    这将创建新的OUT_DIR_PATH 目录并用文件填充它,例如:

    _0.cfe
    _0.cfs
    _0.si
    segments_1
    

    ...或者您在dir 中碰巧拥有的任何文件。

    警告:

    我只将它与默认的IOContext 对象一起使用。上下文还有其他构造函数-不确定它们的作用。我认为它们可以让您更好地控制写入的执行方式。

    【讨论】:

    • 我接受了你的回答,虽然是我自己想出来的,因为你在我之前回答了。
    【解决方案2】:

    同时我自己想通了,并为它创建了一个直截了当的方法:

        @SneakyThrows
        public static void copyIndex(ByteBuffersDirectory ramDirectory, Path destination) {
            FSDirectory fsDirectory = FSDirectory.open(destination);
            Arrays.stream(ramDirectory.listAll())
                    .forEach(fileName -> {
                        try {
                            // IOContext is null because in fact is not used (at least for the moment)
                            fsDirectory.copyFrom(ramDirectory, fileName, fileName, null);
                        } catch (IOException e) {
                            log.error(e.getMessage(), e);
                        }
                    });
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-01-21
      • 2011-12-14
      • 1970-01-01
      • 2018-07-31
      • 1970-01-01
      • 2018-02-03
      • 1970-01-01
      相关资源
      最近更新 更多