lucene-java-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "amigo@max3d.com" <am...@max3d.com>
Subject Re: Lucene working with a DB
Date Tue, 21 Dec 2004 17:38:35 GMT
Hello

I'll just paste the relevant MySQL code, you add the calls to it per 
your needs..it has no checking of anything so better add that as well...
It's possible I didnt copy/paste everything but you should get the idea 
where this is going...

-pedja


------------------------------

....
import java.sql.*;
import lucene stuff...
....

public class  sqlTest {

  public static void main(String[] args) throws Exception {

    String sTable  = args[0];
    String sThing = args[1];
    String indexDir = "/path/to/lucene/index";

  try {
    Analyzer analyzer       = new StandardAnalyzer();
    IndexWriter fsWriter  = new IndexWriter(indexDir, analyzer, false);
    addSQLDoc(fsWriter, sTable, sThing);
    fsWriter.close();
  } catch (Exception e) {
        throw new Exception(" caught a " + e.getClass() + "\n with 
message: " + e.getMessage());
  }
 }

 private void addSQLDoc(IndexWriter writer, String sqlTable, String 
somethingElse) throws Exception {

    String cs             = 
"jdbc:mysql://HOST/DATABASE?user=SQLUSER&password=SQLPASSWORD";
    String sql            = "SELECT * FROM " + sqlTable + " WHERE 
something=\"" + somethingElse + "\"";

    // establish a connection to MySQL database
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
        System.out.println("Lucene: ERROR: Unable to load driver");
        e.printStackTrace();
    }

    // get the record data...
    try {

       Connection conn = DriverManager.getConnection(cs);
       Statement Stmt = conn.createStatement();
       ResultSet RS = Stmt.executeQuery(sql);

       while(RS.next()) {
          // make a new, empty document
          Document doc = new Document();

          // get the database fields
          String field2 = RS.getString(1);
          String field2 = RS.getString(2);
          String field3 = RS.getString(3);
          String field4 = RS.getString(4);
          String field5 = RS.getString(5);

          // add the first group of fields
          //
          doc.add(Field.Keyword("FIELD1", field1));
          doc.add(Field.Keyword("FIELD2", field2));
          doc.add(Field.Keyword("FIELD3", field3));
          doc.add(Field.Keyword("FIELD4", field4));
          doc.add(Field.Text("FIELD5", field5));

          // add the document
          writer.addDocument(doc);

        } catch (Exception e) {
                e.printStackTrace();
                throw new Exception();
        }

       } // close while(..)

       RS.close();
       Stmt.close();
       conn.close();

    } catch(SQLException e) {
        throw new Exception();
    }
  }
}

--------------------------------------------------------------


Daniel Cortes said the following on 12/21/2004 10:39 AM:

> I read a lot of messages that Lucene can index a DB because it use 
> that INPUTSTREAM "type"
> I don't understand how to do this. For example if I've a forum with 
> Mysql  and a lot of files on my web, for every search I've to select 
> the index that I want use in my search, true? But I don't know how to 
> do that Lucene writes an index about the information of the DB of 
> forum (for example  MySQL)
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
> For additional commands, e-mail: lucene-user-help@jakarta.apache.org
>
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: lucene-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: lucene-user-help@jakarta.apache.org


Mime
View raw message