package com.sxkj.cyn.luceneDemo;
import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.LockObtainFailedException;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
import org.wltea.analyzer.lucene.IKQueryParser;
import org.wltea.analyzer.lucene.IKSimilarity;
public class createIndex {
private String driverClassName="net.sourceforge.jtds.jdbc.Driver";
private String url="jdbc:jtds:sqlserver://192.168.187.66:1433/testDB";
private String username="sa";
private String password="123";
public Connection createConn(){
Connection conn=null;
try {
Class.forName(driverClassName).newInstance();
conn = DriverManager.getConnection(url, username, password);
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
return conn;
}
public ResultSet getResult(String sql){
try {
Connection conn=createConn();
PreparedStatement pstmt=conn.prepareStatement(sql);
ResultSet rs=pstmt.executeQuery();
return rs;
} catch (SQLException e) {
e.printStackTrace();
}
return null;
}
public void index(ResultSet rs){
//保存索引文件的地方
String indexDir = "d:\\lucene\\index";
try {
//创建Directory对象 ,FSDirectory代表待索引的文件存在磁盘上
Directory dir = new SimpleFSDirectory(new File(indexDir));
IndexWriter writer=new IndexWriter(dir,new IKAnalyzer(),true,MaxFieldLength.LIMITED);
if(rs!=null){
while(rs.next()){
Document doc=new Document();
doc.add(new Field("id",rs.getString("id"),Store.YES,Index.NO));
doc.add(new Field("userName",rs.getString("userName"),Store.YES,Index.ANALYZED));
doc.add(new Field("userPass",rs.getString("userPass"),Store.YES,Index.ANALYZED));
writer.addDocument(doc);
}
}
System.out.println("numDocs:"+writer.numDocs());
writer.optimize();
writer.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Analyzer getAnalyzer(){
// return new StandardAnalyzer(Version.LUCENE_30);
return new IKAnalyzer();
}
public void seacher(String queryString){
TopDocs hits=null;;
try{
IndexSearcher is = new IndexSearcher(new SimpleFSDirectory(new File("d:\\lucene\\index")));
is.setSimilarity(new IKSimilarity());
// Query query=(new QueryParser(Version.LUCENE_30,"userName",new IKAnalyzer())).parse(queryString);
// Query query = IKQueryParser.parse("userName", queryString);
Query query = IKQueryParser.parseMultiField(new String[]{"userName","userPass"}, queryString);
hits=is.search(query,Integer.MAX_VALUE);
System.out.println("找到了 "+hits.totalHits+" 条记录");
//循环hits.scoreDocs数据,并使用indexSearch.doc方法把Document还原,再拿出对应的字段的值
for (int i = 0; i < hits.scoreDocs.length; i++) {
ScoreDoc sdoc = hits.scoreDocs[i];
Document doc = is.doc(sdoc.doc);
System.out.println("id:"+doc.get("id")+" userName:"+doc.get("userName")+" userPass:"+doc.get("userPass"));
}
}catch(Exception e){
System.out.print(e);
}
}
}