如果要构建一个全文检索系统,并且文章全部保存在数据库中,那下面的例子会有很大的帮助.
import java.sql.*;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;
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.FSDirectory;
//Lucene为数据库建索引实例
public class LuceneDB {
Connection conn = null;
//操作 Oracle 数据库
private final String URL = "jdbc:oracle:thin:@localhost:1521:ORCL";
public LuceneDB(){
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
//Oracle连接用户名:scott 密码:tiger
conn = DriverManager.getConnection(URL,"scott","tiger");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public Connection getConnection(){
return this.conn;
}//获取数据库连接
public void close(){
try {
this.conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}//关闭数据库连接
public static void main(String args[]) throws Exception {
LuceneDB lucene = new LuceneDB();
IndexWriter writer = null;
Connection conn = lucene.getConnection();
String sql = "SELECT * FROM student";
writer = new IndexWriter("D:\\index",new StandardAnalyzer(), true, IndexWriter.MaxFieldLength.LIMITED);
PreparedStatement ps = conn.prepareStatement(sql);
ResultSet rs = ps.executeQuery();
while(rs.next()){
Document doc = new Document();
//为字段 sname 建索引
doc.add(new Field("sname",rs.getString("sname"),Field.Store.YES,Field.Index.ANALYZED));
writer.addDocument(doc);
}
rs.close(); //关闭记录集
conn.close(); //关闭数据库连接
writer.optimize(); //索引优化
writer.close(); //关闭读写器
Directory dir = FSDirectory.getDirectory("D:\\index");
IndexSearcher searcher = new IndexSearcher(dir);
//选择姓名中包含张字的记录
QueryParser parser = new QueryParser("sname", new StandardAnalyzer());
Query query = parser.parse("张");
TopDocs topDocs = searcher.search(query, 100);
ScoreDoc[] hits = topDocs.scoreDocs;
for(int i=0;i< hits.length;i++){
int DocId = hits.doc;
Document doc = searcher.doc(DocId);
System.out.println(doc.get("sname")); //张立
}
}
}
其中,Student表的内容如下:
SNO SNAME SS SAGE SDEPT
----- ---------- -- ---------- ----------
95001 李勇 男 20 CS
95002 刘晨 女 19 IS
95003 王 女 18 MA
95004 张立 男 19 IS
标签:lucene,数据库,new,Lucene,索引,import,apache,org,conn From: https://blog.51cto.com/u_16255870/7552762