I am creating a search page where we can find the product by entering the text.

ex: Brings on the night.

My query bring the records which contain atleast word from this.

Needs: 1. First row should contains the record with the given sentence. 2. second row next most matching. 3. Third row next matching ...etc

How to achieve this. Is there any algorithm for this. It will be more helpful if anyone share your idea.


Sample search Order:

1. Brings on the night
2. Whoever Brings the Night
3. Night Baseball Brings
4. Night ride
5. Night Round
6. Brings flower


Was it helpful?



Building a search engine is a very complex undertaking, dealing with ambiguity, human language, typos, and much more. You should try to use whatever comes with your database engine. SQL Server and SQLite have them out of the box and most other databases probably have similar capabilities. These engines aren't particularly good, but they should suffice for simple scenarios. For more serious work, try Lucene, which comes in various flavors for different programming languages.

As a really simple solution you could use sql's LIKE operator. Instead of

select object_name from table_name where parameter = something

You would do

select object_name from table_name where parameter LIKE something

This might work for very simple scenarios

Some pointers
- try your RDBMS full text search or investigate solutions such as Lucene/Solr
- there are implementations of distance (Levenshtein) in SQL, for not so trivial hand made ranking
- n-grams (bigrams, trigrams) can do a lot, see for example all the options in postgres internal search compared to mysql or MSSQL

Internal RDBMS searches (postgres might be an exception) usually have too little options, implementing your own is usually too hard or RDBMS would not let you do it (efficiently).

In Java you have Lucene

There is also a port for it in php (Zend Lucene).

You also have a port to C# Lucene .NET

Just by changing your db models you can integrate it into the search engine.

Have a look. I've used Lucene in the past and it's always been very effective and efficient.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow