Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Index a MySQL database with Apache Lucene, and keep them synchronized
- public static void main(String[] args) throws Exception {
- Class.forName("com.mysql.jdbc.Driver").newInstance();
- Connection connection = DriverManager.getConnection("jdbc:mysql://localhost/mydb", "root", "");
- StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_36);
- IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_36, analyzer);
- IndexWriter writer = new IndexWriter(FSDirectory.open(INDEX_DIR), config);
- String query = "SELECT id, name FROM tag";
- Statement statement = connection.createStatement();
- ResultSet result = statement.executeQuery(query);
- while (result.next()) {
- Document document = new Document();
- document.add(new Field("id", result.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));
- document.add(new Field("name", result.getString("name"), Field.Store.NO, Field.Index.ANALYZED));
- writer.updateDocument(new Term("id", result.getString("id")), document);
- }
- writer.close();
- }
- writer.deleteAll();
- while (result.next()) {
- Document document = new Document();
- document.add(new Field("id", result.getString("id"), Field.Store.YES, Field.Index.NOT_ANALYZED));
- document.add(new Field("name", result.getString("name"), Field.Store.NO, Field.Index.ANALYZED));
- writer.addDocument(document);
- }
Add Comment
Please, Sign In to add comment