创建索引文件
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.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.wltea.analyzer.lucene.IKAnalyzer;
public class IndexCreateForTable {
/**
* @param args
*/
public static void main(String[] args) {
File f = new File("F:\\lucene\\table");
String url = "";
FSDirectory fs = null;
try {
fs = FSDirectory.open(f);
IKAnalyzer ik = new IKAnalyzer();
ik.setUseSmart(true);
IndexWriterConfig ifg = new IndexWriterConfig(Version.LUCENE_36, ik);
IndexWriter iw = new IndexWriter(fs, ifg);
Class.forName("com.mysql.jdbc.Driver");
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/articles", "root", "root") ;
PreparedStatement ps = conn.prepareStatement("select s.pub_id,s.china_title,s.sponsor from bs_tb_publications s where s.status = '11A'") ;
ResultSet rs = ps.executeQuery();
while(rs.next()){
Document doc = new Document();
doc.add(new Field("id", rs.getString("pub_id"), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("title", rs.getString("china_title"), Field.Store.YES, Field.Index.ANALYZED));
doc.add(new Field("desc", rs.getString("sponsor")==null?"":rs.getString("sponsor"), Field.Store.YES, Field.Index.ANALYZED));
iw.addDocument(doc);
}
iw.commit();
iw.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
搜索程序:
/**
* 多字段搜索
*/
public void test2(){
FSDirectory fs;
try {
fs = FSDirectory.open(new File("F:\\lucene\\table"));
IndexReader ir = IndexReader.open(fs);
long start = System.currentTimeMillis();
IndexSearcher search = new IndexSearcher(ir);
String key = " 卫星 ";
String fieds[] = new String[]{"title","desc"};
IKAnalyzer ik = new IKAnalyzer();
MultiFieldQueryParser m = new MultiFieldQueryParser(Version.LUCENE_36, fieds, ik);
m.setDefaultOperator(Operator.AND);
Query query = m.parse(key) ;
ScoreDoc[] hits = search.search(query, null, 1000).scoreDocs;
System.out.println("共命中"+hits.length+"条记录");
for(ScoreDoc scoreDoc:hits)
{
Document doc= search.doc(scoreDoc.doc);
System.out.println(scoreDoc.score + "\t id:"+doc.get("id")+"\ttitle:"+doc.get("title")+"\tdesc:"+doc.get("desc"));
}
System.out.println("执行时间:"+(System.currentTimeMillis()-start)+"毫秒");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
分享到:
2012-07-31 17:14
浏览 1316
评论