我正在编写一个shell脚本(csh),它必须确定lucene索引版本,然后根据它必须将索引升级到下一个版本。 因此,如果lucene指数在2.x上,我必须将指数升级到3.x. 最后,指数需要升级到6.x。
由于升级指数是一个顺序过程(2.x-> 3.x-> 4.x-> 5.x-> 6.x),我必须事先知道指数版本所以我可以正确设置类路径并升级。
请帮我解决这个问题。
答案 0 :(得分:1)
这不是一个非常干净的解决方案,但这是我能够通过SegmentInfos找到的全部内容。
LuceneVersion - >哪个Lucene代码版本用于此提交, 写成三个vInt:major,minor,bugfix
当您创建IndexReader
时,它是具体的读者类之一,例如 - StandardDirectoryReader,此类有一个toString()
方法,如下所示为每个段打印lucene版本,所以你只需在toString()
个实例上调用 - 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索引版本。
答案 1 :(得分:0)
这是我编写的用于打印索引版本的一段代码。
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版本而言太新或太旧,则将引发异常。异常包含有关版本的信息,可以相应地加以利用。