tags:

views:

1364

answers:

3

I know how to do a regular php mysql search and display the results. However, because of the nature of what I'm trying to accomplish I need to be able to sort by relevancy. Let me explain this better:

Normal Query "apple iphone applications" will search the database using %apple iphone application%, but if there aren't records which display that phrase in that exact order the search will produce nothing.

What I basically need to do is search for 'apple', 'iphone' and 'applications' all separately and then merge the results into one, and then I need to grade the relevancy by how many instances of the word are found in the records. For example if I did what I wanted to do and it returned them following:

Iphone Applications From Apple
Apple Make The Best Apple Iphone Applications
Iphone Applications

They would rank as follows:

Apple Make The Best Apple Iphone Applications
Iphone Applications From Apple
Iphone Applications

Because of how many instances of the search terms are found. See highlighted:

[Apple] Make The Best [Apple] [Iphone] [Applications]
[Iphone] [Applications] From [Apple]
[Iphone] [Applications]

I hope I have explained this well enough and I would be extremely grateful if anyone could give me any pointers.

A: 

A qucik google gave me this link: http://www.mysqlperformanceblog.com/?s=select+match+against+as+relevance

Example:

select title, match (title,content) against (”internet”)as score from cont where match (title,content) against (”internet”) limit 10;`

`

Filip Ekberg
+2  A: 

take a look at the MySQL FULLTEXT search functions, These should automatically return results by relevancy, and give you much more control over your searches

The only potential issue with using fulltext indexes is that they aren't supported by InnoDB tables.

Neil Aitken
A: 
SELECT field2, field3, ..., MATCH(field1, field2) AGAINST ("search string") AS relevance WHERE MATCH(field1, field2) AGAINST "search string" ORDER BY relevance DESC LIMIT 0,10

In the result set, there will be a field "relevance", which is used here to sort the results.

Murat Ayfer