📄 luceneindexdatabase.java
字号:
package chapter5;
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import jeasy.analysis.MMAnalyzer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
/*******************************************************************
* 本代码完成数据库的记录访问和索引。利用JDBC访问数据库记录,利用Lucene建立
* 索引,为后续检索使用做好准备。
*******************************************************************/
public class LuceneIndexDatabase {
private static String Dest_Index_Path = "D:\\workshop\\DataBaseIndex";
private static Connection conn = null;
private static Statement statmt = null;
private static ResultSet record = null;
/*========================================================
* 主函数,指定索引目录和待分析的目录,生成Lucene索引
*========================================================*/
public static void main(String[] args) {
try {
File indexpath = new File(Dest_Index_Path);
IndexWriter Writer = new IndexWriter(indexpath,new StandardAnalyzer(),true);
getGetConn("sa","sa","lucenedb");
record = getResultSet("select * from T_Abstrac");
DbIndexBuilder(Writer);
Writer.optimize();
Writer.close();
} catch (IOException e) {
e.printStackTrace();
}
}
/*========================================================
* 获取数据库连接,取得SQL检索的句柄。
*========================================================*/
public static void getGetConn(String dbname, String user,String passwd) throws IOException{
try {
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver")
.newInstance();
} catch (InstantiationException e2) {
e2.printStackTrace();
} catch (IllegalAccessException e2) {
e2.printStackTrace();
} catch (ClassNotFoundException e2) {
e2.printStackTrace();
}
String url = "jdbc:microsoft:sqlserver://localhost:1433;DatabaseName=" + dbname;
try {
conn = DriverManager.getConnection(url, user, passwd);
} catch (SQLException e2) {
e2.printStackTrace();
}
}
/*========================================================
* 获取记录集,利用指定的语句检索数据库,获取结果
*========================================================*/
public static ResultSet getResultSet(String sqlstate)
throws IOException{
try{
Statement statmt = conn.createStatement();
ResultSet rs = statmt.executeQuery(sqlstate);
return rs;
}
catch(SQLException e){
System.out.println(e);
}
return null;
}
/*========================================================
* 创建数据记录的RAM内存索引,生成并添新文档。合并到本地磁盘索引当中
*========================================================*/
private static void DbIndexBuilder(IndexWriter fswriter)
throws IOException{
while( record.next()){
Directory ramdirectory = new RAMDirectory();
IndexWriter RAMWriter = new IndexWriter(ramdirectory,new StandardAnalyzer(),true);
Document document = new Document();
Field field_id = new Field("Id", record.getString(0),
Field.Store.YES,Field.Index.UN_TOKENIZED);
document.add(field_id);
Field field_name = new Field("Name", record.getString(1),
Field.Store.YES,Field.Index.UN_TOKENIZED);
document.add(field_name);
Field field_source = new Field("Source", record.getString(2),
Field.Store.YES,Field.Index.UN_TOKENIZED);
document.add(field_source);
Field field_abstract = new Field("Abstract", record.getString(3),
Field.Store.YES,Field.Index.UN_TOKENIZED);
document.add(field_abstract);
RAMWriter.addDocument(document);
RAMWriter.close();
fswriter.addIndexes(new Directory[]{ramdirectory});
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -