Do-You-Mean Laravel Search

Image for post
Image for post

There are a number of ways to search your entire table and look for direct matches and retrieve those results. Whenever exact matches can’t be found it is required to return results that are likely to be relevant to a search argument even when the argument does not exactly correspond to the desired information. This a small implementation using Laravel and MySQL.

  1. Laravel
  2. MySQL DB
  3. PhPStrom IDE (Any IDE can be used)

We will use Jaro-Winkler Similarity to match the query value against the data in the table both Products and Application_Doc.

We don’t have to write an implementation for Jaro-Winkler Similarity there exists an amazing Laravel package we just need to add it via composer to our project.

composer require atomescrochus/laravel-string-similarities

The above code will filter based on the Jaro-Winkler Similarity and sort the collection based on similarity with the query against each column in the Product table and suggest a new collection.

The same for the ApplicationDoc table.

The above method will sort suggestions from each table based on the highest similarity.

The above method collects the suggestion and creates a new single dimension array.

We can paginate the result with this method

Add combine all the above in a single controller

Written by

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store