java增量索引,lucene增量索引的简单实现

importjava.io.BufferedReader;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjava.io.PrintWriter;importjava.sql.Connection;importjava.sql.DriverManager;importjava.sql.ResultSet;importjava.sql.Statement;importorg.apache.lucene.analysis.Analyzer;importorg.apache.lucene.analysis.standard.StandardAnalyzer;importorg.apache.lucene.document.Document;importorg.apache.lucene.document.Field;importorg.apache.lucene.index.IndexWriter;publicclassIndex {publicstaticvoidmain(String[] args) {try{

Index index=newIndex();

String path="d:\\index";//索引文件的存放路径String storeIdPath="d:\\storeId.txt";//存储ID的路径String storeId="";

storeId=index.getStoreId(storeIdPath);

ResultSet rs=index.getResult(storeId);

index.indexBuilding(path, storeIdPath, rs);

storeId=index.getStoreId(storeIdPath);

System.out.println(storeId);//打印出这次存储起来的ID}catch(Exception e) {

e.printStackTrace();

}

}publicResultSet getResult(String storeId)throwsException{

Class.forName("com.mysql.jdbc.Driver").newInstance();

String url="jdbc:mysql://localhost:3306/ding";

String userName="root";

String password="ding";

Connection conn=DriverManager.getConnection(url,userName,password);

Statement stmt=conn

.createStatement();

ResultSet rs=stmt

.executeQuery("select * from newitem where id > '"+storeId+"'order by id");returnrs;

}publicbooleanindexBuilding(String path,String storeIdPath, ResultSet rs) {//把RS换成LIST原理一样try{

Analyzer luceneAnalyzer=newStandardAnalyzer();//取得存储起来的ID,以判定是增量索引还是重新索引booleanisEmpty=true;try{

File file=newFile(storeIdPath);if(!file.exists()) {

file.createNewFile();

}

FileReader fr=newFileReader(storeIdPath);

BufferedReader br=newBufferedReader(fr);if(br.readLine()!=null) {

isEmpty=false;

}

br.close();

fr.close();

}catch(IOException e) {

e.printStackTrace();

}

IndexWriter writer=newIndexWriter(path, luceneAnalyzer, isEmpty);//参数isEmpty是false表示增量索引String storeId="";booleanindexFlag=false;

String id;

String title;while(rs.next()) {//for(Iterator it = list.iterator();it.hasNext();){id=rs.getString("id");

title=rs.getString("title");

writer.addDocument(Document(id, title));

storeId=id;//将拿到的id给storeId,这种拿法不合理,这里为了方便indexFlag=true;

}

writer.optimize();

writer.close();if(indexFlag){//将最后一个的ID存到磁盘文件中this.writeStoreId(storeIdPath, storeId);

}returntrue;

}catch(Exception e) {

e.printStackTrace();

System.out.println("出错了"+e.getClass()+"\n   错误信息为:"+e.getMessage());returnfalse;

}

}publicstaticDocument Document(String id, String title) {

Document doc=newDocument();

doc.add(newField("ID", id, Field.Store.YES, Field.Index.TOKENIZED));

doc.add(newField("TITLE", title, Field.Store.YES,

Field.Index.TOKENIZED));returndoc;

}//取得存储在磁盘中的IDpublicstaticString getStoreId(String path) {

String storeId="";try{

File file=newFile(path);if(!file.exists()) {

file.createNewFile();

}

FileReader fr=newFileReader(path);

BufferedReader br=newBufferedReader(fr);

storeId=br.readLine();if(storeId==null||storeId=="")

storeId="0";

br.close();

fr.close();

}catch(Exception e) {

e.printStackTrace();

}returnstoreId;

}//将ID写入到磁盘文件中publicstaticbooleanwriteStoreId(String path,String storeId) {booleanb=false;try{

File file=newFile(path);if(!file.exists()) {

file.createNewFile();

}

FileWriter fw=newFileWriter(path);

PrintWriter out=newPrintWriter(fw);

out.write(storeId);

out.close();

fw.close();

b=true;

}catch(IOException e) {

e.printStackTrace();

}returnb;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值