I am trying to utilize Lucene to develop full text search in my application, which need to build index based on my mysql database. I was wondering is how to keep these index synchronized with db? I came up with to ways: 1) add extra code in business logic tightly to update the search index . 2) running a separated task to rebuild the index periodically.
do you have any other approaches? and what do you think is the best way? Any comments would be appreciate, thanks in advance!
You could setup a trigger in MySQL to update changed docs for all inserted/updated/deleted documents.
Also, you could setup a Filter (javax.servlet spec) in your application to intercept server requests and push them to your index before they even reach database (it can even be done in the same transaction, but there's rarely a real need for that, eventual consistency is usually fine for search engines).
You can, as you said, also schedule periodical updates (similar to what I contributed to DIH in Solr).
If you use Hibernate, check out Hibernate search project. It should cover a lot of boilerplate.
You could use Elastic Search which provides and a JDBC river plugin or manually trigger updates using events on your Entities via JPA.