954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Calculating relevance from a search

Hello all,

I am building a website and on my search page I want to be able to display the relevance of each search result. I've searched all over the place but still haven't found any information on how to do this.And obviously im building this website in PHP. Any ideas?

Thanks!
-maddog39

maddog39
Light Poster
39 posts since Oct 2007
Reputation Points: 10
Solved Threads: 5
 

SELECT * FROM mytable WHERE MATCH(title,content)
AGAINST('+search* ~term' IN BOOLEAN MODE) ORDER BY relevance DESC;

Have a look at this for more information.

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

Okay thanks, i'm taking a look at it now.

maddog39
Light Poster
39 posts since Oct 2007
Reputation Points: 10
Solved Threads: 5
 

Okay I took a look at that page and everything. But I'm a bit confused because its telling you to order by relevance, when thats what I am trying to determine in the first place. Any ideas?

maddog39
Light Poster
39 posts since Oct 2007
Reputation Points: 10
Solved Threads: 5
 
SELECT * FROM mytable WHERE MATCH(title,content)
AGAINST('+search* ~term' IN BOOLEAN MODE) ORDER BY relevance DESC;

No you, the person writing the query, are telling MySQL to return the results in order of relevance. The +sign means that all words must contain the word search. The * is a wildcard meaning that searched, searching, and searchable will be returned. If the record also contains the word term it will be returned and will be considered less relevant than those which dont contain it, due to the ~ operator. If you want the word term to increase the relevancy, dont use the ~.
MySQL uses your query as a kind of voting system. How you arrange your query and which operators you use, determines which row gets the most number of votes. The row with the most votes gets returned first.

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

Yea i understand all of that except the ORDER BY relevance part, where are you (am I) getting that field. Where's it coming from? Thats where I'm confused.

maddog39
Light Poster
39 posts since Oct 2007
Reputation Points: 10
Solved Threads: 5
 

the relevance? It looks as though the query generates it.

scru
Posting Virtuoso
1,629 posts since Feb 2007
Reputation Points: 975
Solved Threads: 140
 

I found another way to do this.
The table name is Employees . The field name is Role. The query finds all rows in Role that contain an A and the word Sales and returns them in order of relevancy.

SELECT *, MATCH(Role)
AGAINST('+A* *Sales' IN BOOLEAN MODE)AS relevance FROM Employees ORDER BY relevance DESC;

Hopefully you can manipulate this code and get it to work for you.

buddylee17
Practically a Master Poster
697 posts since Nov 2007
Reputation Points: 232
Solved Threads: 137
 

Alright thanks alot. Looks like that query did it for me.

maddog39
Light Poster
39 posts since Oct 2007
Reputation Points: 10
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You