package Job;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.Term;
public class ManageIndex {
// 初始化索引以及进行增量索引
public static void InitAndAddIndex(String indexDir, Connection conn)throws IOException, SQLException {
PreparedStatement pstm = null;
ResultSet rs = null;
String posID = "0";
try {
FileReader fr = new FileReader("D:\\TestLucene\\num.txt");
BufferedReader br = new BufferedReader(fr);
posID = br.readLine();
if (posID == null || posID == "")
posID = "0";
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
int posid=Integer.parseInt(posID);
String sql = "select ComPosition.posID,ComPosition.comName,ComPosition.posJobFun1,ComPosition.posJobFun2, ComPosition.posJobFun3, ComPosition.posName, ComPosition.deptID, ComPosition.posNum," +
"ComPosition.posJobLocal1, ComPosition.posJobLocal2, ComPosition.posJobLocal3, ComPosition.posReqDegreeID, ComPosition.posReqWorkYear, ComPosition.posFiltDegree," +
"ComPosition.posFiltWorkYear, ComPosition.posReqSex, ComPosition.posReqAge1,ComPosition.posReqAge2, ComPosition.posAddressPC," +
"ComPosDesc.posRequest, ComPosDesc.posApplyDesc, ComPosDesc.posPerson, ComPosDesc.posTel, ComPosDesc.posTelShowFlag, ComPosDesc.posFax," +
"ComPosDesc.posFaxShowFlag, ComPosDesc.posEmail, ComPosDesc.posMailShowFlag, ComPosDesc.posMobile, ComPosDesc.posMoShowFlag," +
"ComPosDesc.posAddress, ComPosDesc.posZipCode from ComPosition , ComPosDesc where ComPosition.posID=ComPosDesc.posID and ComPosition.posID > "+posid;
try {
pstm = conn.prepareStatement(sql);
rs = pstm.executeQuery();
IndexWriter writer;
boolean isEmpty = true;
try{
FileReader fr = new FileReader("D:\\TestLucene\\num.txt");
BufferedReader br = new BufferedReader(fr);
if(br.readLine()!= null) {
isEmpty = false;
}
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("--------------If recreate index or add index: "+isEmpty);
writer = new IndexWriter(indexDir, new StandardAnalyzer(),isEmpty);
writer.setUseCompoundFile(true);
while (rs.next()) {
Document doc = new Document();
doc.add(new Field("posID", String.valueOf(rs.getInt("posID")),Field.Store.YES, Field.Index.TOKENIZED));//职位编号
// doc.add(new Field("comID", String.valueOf(rs.getInt("comID")),Field.Store.NO, Field.Index.TOKENIZED));//企业编号
doc.add(new Field("comName", rs.getString("comName"),Field.Store.YES, Field.Index.TOKENIZED)); //企业名称
// doc.add(new Field("posCall", rs.getString("posCall"),Field.Store.NO, Field.Index.TOKENIZED)); //职位所属行业
doc.add(new Field("posJobFun1", rs.getString("posJobFun1"),Field.Store.YES, Field.Index.TOKENIZED)); //岗位类别
doc.add(new Field("posJobFun2", String.valueOf(rs.getInt("posJobFun2")),Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("posJobFun3", String.valueOf(rs.getInt("posJobFun3")),Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("posName", rs.getString("posName"),Field.Store.YES, Field.Index.TOKENIZED)); //职位名称
doc.add(new Field("deptID", rs.getString("deptID"),Field.Store.YES, Field.Index.TOKENIZED)); //所属部门
doc.add(new Field("posNum", rs.getString("posNum"),Field.Store.YES, Field.Index.TOKENIZED)); //招聘人数
doc.add(new Field("posJobLocal1", String.valueOf(rs.getInt("posJobLocal1")),Field.Store.YES, Field.Index.TOKENIZED));//工作地区1
doc.add(new Field("posJobLocal2", rs.getString("posJobLocal2"),Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("posJobLocal3", rs.getString("posJobLocal3"),Field.Store.YES, Field.Index.TOKENIZED));
doc.add(new Field("posReqDegreeID", rs.getString("posReqDegreeID"),Field.Store.YES, Field.Index.TOKENIZED)); //学历要求
doc.add(new Field("posReqWorkYear", String.valueOf(rs.getInt("posReqWorkYear")),Field.Store.YES, Field.Index.TOKENIZED)); //工作经验要求
doc.add(new Field("posFiltDegree", String.valueOf(rs.getInt("posFiltDegree")),Field.Store.NO, Field.Index.TOKENIZED)); //是否过滤学历要求
doc.add(new Field("posFiltWorkYear", String.valueOf(rs.getInt("posFiltDegree")),Field.Store.NO, Field.Index.TOKENIZED)); //是否过滤工作经验要求
// doc.add(new Field("posReqLocation", rs.getString("posReqLocation"),Field.Store.NO, Field.Index.TOKENIZED)); //现所在地要求
doc.add(new Field("posReqSex", rs.getString("posReqSex"),Field.Store.YES, Field.Index.TOKENIZED)); //性别要求
doc.add(new Field("posReqAge1", String.valueOf(rs.getInt("posReqAge1")),Field.Store.YES, Field.Index.TOKENIZED));//年龄下限
doc.add(new Field("posReqAge2", String.valueOf(rs.getInt("posReqAge2")),Field.Store.YES, Field.Index.TOKENIZED));//年龄上限
// doc.add(new Field("posState", rs.getString("posState"),Field.Store.NO, Field.Index.TOKENIZED));//职位状态
// doc.add(new Field("posFlag", rs.getString("posFlag"),Field.Store.NO, Field.Index.TOKENIZED)); //职位标志
// doc.add(new Field("posHitCount", String.valueOf(rs.getInt("posHitCount")),Field.Store.NO, Field.Index.TOKENIZED)); //职位电击次数
// doc.add(new Field("posResumeNum", rs.getString("posResumeNum"),Field.Store.NO, Field.Index.TOKENIZED)); //收到的简历数
doc.add(new Field("posAddressPC", rs.getString("posAddressPC"),Field.Store.YES, Field.Index.TOKENIZED)); //职位工作地区
doc.add(new Field("posRequest", rs.getString("posRequest"),Field.Store.YES, Field.Index.TOKENIZED)); //职位要求
doc.add(new Field("posApplyDesc", rs.getString("posApplyDesc"),Field.Store.YES, Field.Index.TOKENIZED)); //面试需知
doc.add(new Field("posPerson", rs.getString("posPerson"),Field.Store.YES, Field.Index.TOKENIZED));//联系人
doc.add(new Field("posTel", rs.getString("posTel"),Field.Store.YES, Field.Index.TOKENIZED)); //联系人电话
// doc.add(new Field("posTelShowFlag", rs.getString("posTelShowFlag"),Field.Store.NO, Field.Index.TOKENIZED)); //是否过滤电话标志
doc.add(new Field("posFax", rs.getString("posFax"),Field.Store.YES, Field.Index.TOKENIZED)); //传真
// doc.add(new Field("posFaxShowFlag", rs.getString("posFaxShowFlag"),Field.Store.NO, Field.Index.TOKENIZED)); //是否过滤传真标志
doc.add(new Field("posEmail", rs.getString("posEmail"),Field.Store.YES, Field.Index.TOKENIZED));//电子邮件
// doc.add(new Field("posMailShowFlag",rs.getString("posMailShowFlag"),Field.Store.NO, Field.Index.TOKENIZED));//是否过滤邮件标志
doc.add(new Field("posMobile", rs.getString("posMobile"),Field.Store.YES, Field.Index.TOKENIZED));//手机
// doc.add(new Field("posMoShowFlag",rs.getString("posMoShowFlag"),Field.Store.NO, Field.Index.TOKENIZED));//是否过滤手机标志
doc.add(new Field("posAddress", rs.getString("posAddress"),Field.Store.YES, Field.Index.TOKENIZED));//地址
doc.add(new Field("posZipCode", rs.getString("posZipCode"),Field.Store.YES, Field.Index.TOKENIZED)); //邮编
writer.addDocument(doc);
posid++;
}
System.out.println("---------------建立的索引记录数为: "+writer.docCount());
writer.optimize();
writer.close();
try {
FileWriter fw = new FileWriter("D:\\TestLucene\\num.txt");
PrintWriter out = new PrintWriter(fw);
out.print(posid);
评论0