TSql Contains weighed columns
I have the following开发者_StackOverflow中文版 request:
SELECT Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId
FROM V_ProductForSearch
WHERE CONTAINS ((SearchKeyWords, PTitle, PDescription, Presentation), @KeyWords)
Is it possible to attribute a different weight in function of the column where the term was found. Per example, in this case it would be a weight of 0.8 for the two first columns and 0.5 for the last ones.
I know it is possible to assign a weight when we have several searched terms, but is it possible to do the same thing for columns ?
Microsoft has not implimented a way to achieve this, you can only weigh the keywords as it has been implimented. http://msdn.microsoft.com/en-us/library/ms187787.aspx (Read paragraph G.)
I have tried to make an alternative way of doing this, I hope it can be used.
What i am doing is weighing SearchKeyWords = 5, PTitle = 4, PDescription = 3, Presentation = 2. These values are being summed up and ordered by
SELECT Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId, SUM(srank) ranked
FROM (
SELECT Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId, 5 srank
FROM V_ProductForSearch
WHERE CONTAINS ((SearchKeyWords), @KeyWords)
UNION ALL
SELECT Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId, 4 srank
FROM V_ProductForSearch
WHERE CONTAINS ((PTitle), @KeyWords)
UNION ALL
SELECT Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId , 3 srank
FROM V_ProductForSearch
WHERE CONTAINS ((PDescription), @KeyWords)
UNION ALL
SELECT Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId, 2 srank
FROM V_ProductForSearch
WHERE CONTAINS ((Presentation), @KeyWords) ) a
GROUP BY Id,SearchKeyWords, PTitle, PDescription, Presentation, Category, ManufacturerId
ORDER BY SUM(srank)
精彩评论