由于Lucene文件格式从2到3以及从3到4版本时都发生了重大的改变,造成了高版本无法读取低版本的数据,使用Lucene中的IndexUpgrader方法先将版本从2升到3,然后再从3升级到4。
import java.io.File;
import java.io.IOException;
import java.util.List;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexUpgrader;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class ConvertFromTwo {
public static void main(String[] args) {
ConvertFromTwo c = new ConvertFromTwo();
c.upgrade();
}
private final String INDEX_PATH = "C:/Users/rainystars/Desktop/index-synonym";
public void upgrade(){
try {
File dir = new File(INDEX_PATH);
String path = "";
String[] dirList = dir.list();
for (String d:dirList){
path = INDEX_PATH + "/" + d;
IndexUpgrader t = new IndexUpgrader(FSDirectory.open(new File(path)),
Version.LUCENE_36,null,true);
t.upgrade();
System.out.println(path+" Done.");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("All Done.");
}
}
从版本2升级到版本3时,需要使用lucene3的jar包,我使用的lucene3.6的jar包,我需要处理的索引是在一个文件夹中所存在的一系列索引文件,所以需要循环来遍历每个目录。
import java.io.File;
import java.io.IOException;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexUpgrader;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
public class ConvertFromThree {
public static void main(String[] args) {
ConvertFromThree c = new ConvertFromThree();
c.upgrade();
}
private final String INDEX_PATH = "C:/Users/rainystars/Desktop/index-synonym";
public void upgrade(){
try {
File dir = new File(INDEX_PATH);
String path = "";
String[] dirList = dir.list();
for (String d:dirList){
path = INDEX_PATH + "/" + d;
//System.out.println(path);
IndexUpgrader t = new IndexUpgrader(FSDirectory.open(new File(path)),
Version.LUCENE_44,System.out,true);
t.upgrade();
System.out.println(path+" Done.");
}
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("All Done.");
}
}
在3到4版本的转化中使用的是lucene4.4的jar包