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

2 comments:

  1. Hi,

    This is Anush.
    Index writer not accepting for IndexWri(String,anlyzer,boolean,length);

    How to resolve this.

    ReplyDelete
  2. Hey Anush,

    The post is very old, targeting Lucene 2.X version. The API has changed now, if you are using Lucene 4.x please refer to the following link for the correct IndexWriter ctor() signature. http://lucene.apache.org/core/4_0_0/core/org/apache/lucene/index/IndexWriter.html#IndexWriter(org.apache.lucene.store.Directory, org.apache.lucene.index.IndexWriterConfig)

    ReplyDelete