Lucene and MySQL (Correction!)

In my


public void GetSqlData() {
String IndexStoreDir = "F:/NewsIndex";
StandardAnalyzer analyzer = new StandardAnalyzer();
Connection con = null;
Statement stmt = null;
ResultSet rs=null;
try {
String connUrl = "jdbc:mysql://localhost:3306/SQLDb";
// put your mysql database name in place of SQLDb
Connection con = DriverManager.getConnection(connUrl, "root", "SQLPassword");
// put your mysql password in place of SQLPassword
stmt = con.createStatement();
Class.forName("com.mysql.jdbc.Driver");
stmt.executeQuery("select * from table");
IndWri = new IndexWriter(IndexStoreDir, analyzer, false, IndexWriter.MaxFieldLength.UNLIMITED);
int numberOfUpdates=0;
rs = stmt.getResultSet();

while (rs.next()) {
Document docu = new Document();
Field title= new Field("title", rs.getString("title"), Field.Store.YES, Field.Index.ANALYZED);
Field content= new Field("content", rs.getString("content"), Field.Store.YES, Field.Index.ANALYZED);
docu.add(title);
docu.add(content);
IndWri.addDocument(docu);
}
IndWri.close();

} catch (Exception ex) {
}
finally {
if(rs!=null){try{rs.close();} catch(Exception ex2){}}
if(stmt!=null){try{stmt.close();} catch(Exception ex1){}}
if(con != null){try{con.close(); }catch(Exception e){}}

}
}


There is no need of addDoc() method. In previous case the index was getting created for (number of fields)*(number of records) number of docs because every time addDoc() method was called which created a new document for a field.

Spread The Love, Share Our Article

Related Posts

No Response to "Lucene and MySQL (Correction!)"