- When a new item is added to MySQL, it must also be indexed by Lucene.
- When an existing item is deleted from MySQL, it must also be removed from the Lucene index.
The idea is to write a script that will be called every x minutes through the scheduler (for example, the CRON task). This is a way to synchronize MySQL and Lucene. What I have managed so far:
- For each newly added item in MySQL, Lucene also indexes it.
- For every item already added in MySQL, Lucene does not reindex it (there are no duplicated items).
This is what I ask you to help about:
- For every previously added item that has been removed from MySQL, Lucene must also disable it.
Here is the code I used that tries to index the MySQL tag (id [PK] | name) table tag (id [PK] | name) :
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(); }
PS: this code is for testing purposes only, no need to tell me how awful it is :)
EDIT:
One solution may be to delete any previously added document and reindex the entire database:
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); }
I'm not sure if this is the most optimized solution, is it?
java synchronization mysql indexing lucene
sp00m
source share