views:

82

answers:

6

Hi All,

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.

Edit:

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

Geetha

+1  A: 

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.

Marcelo Cantos
A: 

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

Ankur
I am doing the something. But i am not getting the proper order.
Geetha
You can use ORDER BY - http://www.1keydata.com/sql/sqlorderby.html
Ankur
Ok I see what you mean. Well it's a very simple solution. If you want to have them in order then you need to define some measure of "matchingness" let m be the degree to which x and y match. This is not simple and I don't think I can give you an answer through this textbox. If you really want this to work you should probably investigate Lucene as recommended by Marcelo
Ankur
When I say "it's a simple solution", I mean what I gave in this answer was a simple solution, and hence doesn't necessarily meet the ordering requirements. To meet your needs you will probably need something reasonably sophisticated.
Ankur
A: 

Have you tried full-text search? http://msdn.microsoft.com/en-us/library/ms142583.aspx

Mika Kolari
A: 

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).

Unreason
A: 

Go to google.com

James Wilkinson
A: 

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.

Jamie