04 October 2009

Lucene and MySQL (Correction!)

NOTE: This is just a correction post of Lucene and MySQL, please read that before running the following code.

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();
            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);
        } 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.