Jump to content

Recommended Posts

OK... I Have a query here... I need this:
I need these to have an importance rate, where order of importance is as follows:
1. artist
2. title
3. album
4. lyric

artist is heaviest, and should be displayed higher in the returned results.
So... My question is... How do I get them to weigh what I want?

[code]$query = "SELECT artist,lyric,title,album,
MATCH(artist, album, title, lyric) AGAINST ('$trimmed' IN BOOLEAN mode)
AS score
FROM lyrics
WHERE 
MATCH(artist, album, title, lyric) AGAINST ('$trimmed' IN BOOLEAN mode) ORDER BY score DESC";[/code]
Link to comment
https://forums.phpfreaks.com/topic/28015-mysql-order-of-importance/
Share on other sites

could simply run it as four searches.  Serch artist first, then title etc.... Have each search add on to the previous results in a table/list/whatever..... Thats the simplest way, but if your pulling large amouts form your table, or have large numbers of quieryes running syimoltainusly it could slow things down.  Depending on what database your using it would have to be quite a lot though! (maybe if its large you should consider postgreSQL?)

Alternitivly asign a number to each (artist = 1, title = 2 etc), and have your database sort its table numericaly.

Hope this helps
Or, just compute the four different scores in one query and do the weighting within the php
[code]$query = "SELECT artist,lyric,title,album,
MATCH(artist) AGAINST ('$trimmed' IN BOOLEAN mode)
AS score1
MATCH(album) AGAINST ('$trimmed' IN BOOLEAN mode)
AS score2
MATCH(title) AGAINST ('$trimmed' IN BOOLEAN mode)
AS score3
MATCH(lyric) AGAINST ('$trimmed' IN BOOLEAN mode)
AS score4
FROM lyrics
WHERE 
MATCH(artist, album, title, lyric) AGAINST ('$trimmed' IN BOOLEAN mode) ORDER BY score DESC";[/code]

Then in the php calculate $score as ($score1 * 4) + ($score2 * 3) + ($score3 * 2) + ($score4 * 1)

or whatever weighting you want.
[code]$query = "SELECT artist,lyric,title,album,
MATCH(artist) AGAINST ('$trimmed' IN BOOLEAN mode) AS score1,
MATCH(album) AGAINST ('$trimmed' IN BOOLEAN mode) AS score2,
MATCH(title) AGAINST ('$trimmed' IN BOOLEAN mode) AS score3,
MATCH(lyric) AGAINST ('$trimmed' IN BOOLEAN mode) AS score4
FROM lyrics
WHERE 
MATCH(artist, album, title, lyric) AGAINST ('$trimmed' IN BOOLEAN mode) ORDER BY score1 ASC";[/code]

I modified your so it works didn't bring up errors...

for [b]ORDER BY score1[/b], do I need to make it like it is now or like this:
[b]ORDER BY score1, score2, score3, score4 ASC[/b]

Now what do you mean use php to calculate $score?  I have never done something like that.
Well, many times when doing such a ranking you display the results with a value indicating their overall relevance. In this case you have four different numbers. What I was trying to say is that you could come up with a "total" score based upon whatever weight you want to assign to each field match. In my example I was weighting the first field 4x, the 2nd field 2x, the 3rd fiel 2x and the 4th field at 1x. You could do those calculations and add it all up to come up with a combined score if you so wish.

Also, in your query, you should add sorting for the 2nd, 3rd, & 4th values as well.
I don't know what your doing with creating a boolean relevance search in that manner, it doesn't make much sense to do the same search repeatably, when all your wanting to do is order / return the listing by field order, based on the matches found and return the [b]single relevance value for each relevance ordered row returned[/b]. If that's not want your trying to do then please disregard my comment. If that is what your trying to do, tell me and I'll show how to do it the right way.


printf
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.