【发布时间】:2017-05-24 10:35:52
【问题描述】:
我正在编写一个 shell 脚本 (csh),它必须确定 lucene 索引版本,然后根据它必须将索引升级到下一个版本。 因此,如果 lucene 索引在 2.x 上,我必须将索引升级到 3.x 最后索引需要升级到 6.x。
由于升级索引是一个顺序过程(2.x->3.x->4.x->5.x->6.x),我必须事先知道索引版本,以便我可以设置正确的类路径并升级。
请帮帮我。
【问题讨论】:
我正在编写一个 shell 脚本 (csh),它必须确定 lucene 索引版本,然后根据它必须将索引升级到下一个版本。 因此,如果 lucene 索引在 2.x 上,我必须将索引升级到 3.x 最后索引需要升级到 6.x。
由于升级索引是一个顺序过程(2.x->3.x->4.x->5.x->6.x),我必须事先知道索引版本,以便我可以设置正确的类路径并升级。
请帮帮我。
【问题讨论】:
这不是一个非常干净的解决方案,但这就是我可以通过SegmentInfos 找到的所有解决方案。
LuceneVersion --> 这次提交使用了哪个 Lucene 代码版本, 写成三个 vInt:major、minor、bugfix
当您创建 IndexReader 时,它是具体的阅读器类之一 - StandardDirectoryReader 并且此类具有如下所示的 toString() 方法,该方法为每个段打印 lucene 版本,因此您可以简单地调用 - @987654325 @ 在IndexReader 实例上。
@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 索引版本。
【讨论】:
这是我为打印索引版本而编写的一段代码。
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 版本而言太新或太旧的索引,则会引发异常。异常包含有关可以相应利用的版本的信息。
【讨论】: