php lucene索引,Lucene索引升级

由于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包

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值