Search code examples
mysqlsearchelasticsearchinstant

MySQL search with typo


In my MySQL database I have a user table. I need to perform search as you type with typo over the user name field. There are few very old question on this topic. I tested the builtin full text search of mysql but it didn't work as expected (it does not handle typo) [I knew but I tried anyway]. What's my best option? I thought there should be an easy solution nowadays. I'm thinking about replicating the user table on elasticsearch and do the instant search from there, but I'd really like to avoid the syncronization nightmare that this will cause.

Thanks!!


Solution

  • You could use SOUNDEX for mysql. We have tried that but I can say that it does not work that well and it also makes the search a bit slow.

    We Had a similar issue and switched to ES.

    What we did is as follows:

    • Created a trigger for the table that will be synced to ES. The trigger will write to a new table. The columns of such a table would be:

      IdToUpdate Operation DateTime IsSynced

      The Operation would be create, update, delete. IsSynced will tell whether the update is pushed to ES.

    • Then add a corn job that would query this table for all rows that will have issynced set to say '0', Add those ID's and operation to a Queue like RabbitMQ. And set the ISSynced to 1 for those ID's

      The reason to use RabbitMQ is that it will make sure that the update is forwarded to ES. In case of failure we can always re-queue the the object.

    • Write a consumer to get the objects from the queue and update ES.

    Apart from this you will also have to create a utility that will create an ES index from the database for first time use.

    And you can also look at Fuzzy Search of ES that will handle typo's

    Also Completion suggester which also supports fuzzy search.