我正在将一个 lucene 3.6 搜索 API 集成到一个 java 桌面应用程序中。lucene 系统使用文件系统目录来存储索引。创建索引目录、索引编写器并将文档添加到索引的代码。
索引的数据是从 derby 数据库收集的。数据库表的字段作为字段添加到 lucene 文档中。因此数据库表中的每一行都表示为单个 lucene 文档。
我的问题是,有没有办法检查索引目录,如果它没有填充 lucene 文档,那么填充它。或者在索引已经填充时跳过重新填充索引。
创建索引文件的代码。
public File createIndexDir() throws IOException, SQLException
{
//Check if directory exist
if(!userDir.exists())
{ userDir.mkdir();
System.out.println(" Index directory created at " + userDir.getAbsolutePath());
}
return userDir.getAbsoluteFile();
}
创建索引编写器的代码
public void createIndexWriter() throws IOException, SQLException
{
indexDir = createIndexDir();
if(iw == null)
{
try {
// create some index
StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
IndexWriterConfig IWConfig = new IndexWriterConfig(Version.LUCENE_36, analyzer);
iw = new IndexWriter(FSDirectory.open(indexDir), IWConfig);
}
catch (CorruptIndexException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
} catch (LockObtainFailedException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(Indexer.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
这是用数据库中的数据填充索引文件的代码
public void buildIndex () throws SQLException, CorruptIndexException, IOException
{
/* Connecting to the database */
Connection con = DriverManager.getConnection(host, uName, uPass);
Statement stmt = con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
String sql = "SELECT * FROM APP.REGISTRY";
ResultSet rs = stmt.executeQuery(sql);
rs.beforeFirst(); //set poinyrt to begining of result set
while(rs.next())
{
Document doc = new Document();
doc.add(new Field("id",rs.getString("ID"),Field.Store.YES,Field.Index.NO));
if(rs.getString("SUBJECT")== null)
{ doc.add(new Field("subject","",Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("subject",rs.getString("SUBJECT"),Field.Store.YES,Field.Index.ANALYZED));
}
if(rs.getString("LETTER_FROM")== null)
{ doc.add(new Field("letter_from"," ",Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("letter_from",rs.getString("LETTER_FROM"),Field.Store.YES,Field.Index.ANALYZED));
}
doc.add(new Field("date_of_letter",DateTools.dateToString(rs.getDate("DATE_OF_LETTER"),
DateTools.Resolution.DAY),Field.Store.YES,Field.Index.ANALYZED));
doc.add(new Field("date_received",DateTools.dateToString(rs.getDate("DATE_RECEIVED"),
DateTools.Resolution.DAY),Field.Store.YES,Field.Index.NO));
if(rs.getString("REMARKS")== null)
{ doc.add(new Field("remarks"," ",Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("remarks",rs.getString("REMARKS"),Field.Store.YES,Field.Index.ANALYZED)); }
if(rs.getDate("DATE_DISPATCHED")== null)
{ doc.add(new Field("date_dispatched",DateTools.dateToString(new Date(0L),DateTools.Resolution.DAY),Field.Store.YES,Field.Index.ANALYZED)); }
else {
doc.add(new Field("date_dispatched",DateTools.dateToString(rs.getDate("DATE_DISPATCHED"),
DateTools.Resolution.MINUTE),Field.Store.YES,Field.Index.ANALYZED));
}
if(rs.getString("OFFICE_DISPATCHED_TO")== null)
{ doc.add(new Field("office_dispatched_to"," ",Field.Store.YES,Field.Index.ANALYZED));}
else {
doc.add(new Field("office_dispatched_to",rs.getString("OFFICE_DISPATCHED_TO"),Field.Store.YES,Field.Index.ANALYZED));
}
iw.addDocument(doc);
}
iw.commit();
closeIndexWriter();
stmt.close();
rs.close();
con.close();
}
任何解决方案的想法。为所有人喝彩。