如何确定Lucene索引版本?

4
我正在编写一个shell脚本(csh),需要确定lucene索引的版本,然后根据此版本将其升级到下一个版本。因此,如果lucene索引是2.x,则必须将索引升级到3.x,最终将索引升级到6.x。
由于升级索引是一个顺序过程(2.x->3.x->4.x->5.x->6.x),因此我必须事先知道索引版本,以便可以正确设置类路径并进行升级。
请帮助我完成这个任务。
2个回答

1
这不是一个很干净的解决方案,但这是我能通过SegmentInfos找到的全部信息。

LuceneVersion --> 这个提交使用了哪个Lucene代码版本,写成三个vInt:主要版本、次要版本、错误修复

当您创建IndexReader时,它是像StandardDirectoryReader这样的具体读取器类之一,并且该类有一个toString()方法,如下所示,它会打印每个段的lucene版本,因此您可以在IndexReader实例上简单地调用toString()
@Override public String toString() { final StringBuilder buffer = new StringBuilder(); buffer.append(getClass().getSimpleName()); buffer.append('('); final String segmentsFile = segmentInfos.getSegmentsFileName(); if (segmentsFile != null) { buffer.append(segmentsFile).append(":").append(segmentInfos.getVersion()); } if (writer != null) { buffer.append(":nrt"); } for (final LeafReader r : getSequentialSubReaders()) { buffer.append(' '); buffer.append(r); } buffer.append(')'); return buffer.toString(); }

我猜,针对整个索引使用单一版本没有意义,因为索引可能包含从先前版本编写程序提交的文档。

使用旧版Lucene编写程序提交的文档可以通过最新版本读取器进行搜索,只要版本距离不远,如Lucene所定义。

您可以使用正则表达式在Core Java中编写简单的逻辑,以提取最高的Lucene版本作为您的Lucene索引版本。


1

这是我编写的一段代码,用于打印索引版本。

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexFormatTooNewException;
import org.apache.lucene.index.IndexFormatTooOldException;
import org.apache.lucene.index.StandardDirectoryReader;
import org.apache.lucene.store.SimpleFSDirectory;
import org.junit.Test;

public class TestReindex {

    public void testVersion() throws IOException{
        Path path = Paths.get("<Path_to_index_files>");

        try (DirectoryReader reader = StandardDirectoryReader.open(new SimpleFSDirectory(path))){
            Pattern pattern = Pattern.compile("lucene.version=(.*?),");

            Matcher matcher = pattern.matcher(reader.toString());
            if (matcher.find()) {
                System.out.println("Current version: " + matcher.group(1));
            }
        } catch(IndexFormatTooOldException ex) {
            System.out.println("Current version: " + ex.getVersion());
            System.out.println("Min Version: " + ex.getMinVersion());
            System.out.println("Max Version: " + ex.getMaxVersion());
        } catch (IndexFormatTooNewException ex) {
            System.out.println("Current version: " + ex.getVersion());
            System.out.println("Min Version: " + ex.getMinVersion());
            System.out.println("Max Version: " + ex.getMaxVersion());
        }
    }
}

如果您正在尝试使用与所使用的Lucene版本过新或过旧的索引进行阅读,则会抛出异常。这些异常包含有关版本的信息,可以相应地加以利用。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接