I'm trying to create a keyword search on three fields title, author and text. I want them sorted by relevance so I give one point for a match in any of the fields. I also have a table called tags that stores in each row a particular tag for a story. If there is a match on tag, I add to the relevance how many times the tag was used. When a user enters a search, he may enter a sequence of keywords. I was thinking of exploding the string and using a for loop to add for each keyword the code in the select statement below. This seems rather complicated so I was wondering if there is a better way to do this?
select text, ((case when S.sid in (select sid from tags where tahname like 'db')
then (select uses from tags T where S.sid = T.sid and tagname like 'db') +
(case when title like '%db%' then 1 else 0 end) +
(case when author like '%db%' then 1 else 0 end) +
(case when text like '%db%' then 1 else 0 end)) as relevance
from stories S
order by relevance desc
Thank you.