droppedeye
Programmer
I am trying to write a very simple search page where a user can search for images stored in the database by typing a search term. Images in the database have three attributes that lend themself to searching: Category, Keyword, and Description. I want to compare the search term the user entered to each one of the fields.
Now all that is just fine, except I want the results to be sorted by relevance. In this situation, I have defined the relevance scale to be 3 points if found in Category, 2 points if found in Keyword, and 1 point if found in Description. This means the highest possible relevance for an image would be 6, if the search term was found as a Category, Keyword, and Description of that image. For example, an image with the keyword "deer" (2 points) would be returned before an image with the description "Field of deer grass" (1 point).
Here is what I have so far, its close but its actually returning a result for each time an image is found in different attributes. For example, the query is returning...
WIID: 2 Relevance: 3
WIID: 2 Relevance: 2
WIID: 3 Relevance: 2
WIID: 4 Relevance: 2
WIID: 2 Relevance: 1
WIID: 3 Relevance: 1
WIID: 5 Relevance: 1
...when it should return
WIID: 2 Relevance: 6
WIID: 3 Relevance: 3
WIID: 4 Relevance: 2
WIID: 5 Relevance: 1
I apologize in advance for the size of the query, hopefully someone can make sense of it and provide a solution for me! I am using MSSQL 2000 if that is helpful. Thanks!
Now all that is just fine, except I want the results to be sorted by relevance. In this situation, I have defined the relevance scale to be 3 points if found in Category, 2 points if found in Keyword, and 1 point if found in Description. This means the highest possible relevance for an image would be 6, if the search term was found as a Category, Keyword, and Description of that image. For example, an image with the keyword "deer" (2 points) would be returned before an image with the description "Field of deer grass" (1 point).
Here is what I have so far, its close but its actually returning a result for each time an image is found in different attributes. For example, the query is returning...
WIID: 2 Relevance: 3
WIID: 2 Relevance: 2
WIID: 3 Relevance: 2
WIID: 4 Relevance: 2
WIID: 2 Relevance: 1
WIID: 3 Relevance: 1
WIID: 5 Relevance: 1
...when it should return
WIID: 2 Relevance: 6
WIID: 3 Relevance: 3
WIID: 4 Relevance: 2
WIID: 5 Relevance: 1
I apologize in advance for the size of the query, hopefully someone can make sense of it and provide a solution for me! I am using MSSQL 2000 if that is helpful. Thanks!
Code:
SELECT Image.WIID, (CASE WHEN Title LIKE '%deer%' THEN 2 ELSE 0 END) AS relevance
FROM Image INNER JOIN ImageHasKeywords ON Image.WIID = ImageHasKeywords.WIID INNER JOIN Keyword ON ImageHasKeywords.KeywordID = Keyword.KeywordID
WHERE Title LIKE '%deer%' GROUP BY Image.WIID, Title
UNION
SELECT Image.WIID,(CASE WHEN Description LIKE '%deer%' THEN 1 ELSE 0 END) AS relevance
FROM Image INNER JOIN ImageDetails ON Image.WIID = ImageDetails.WIID
WHERE Description LIKE '%deer%'
UNION
SELECT Image.WIID, (CASE WHEN Title LIKE '%deer%' THEN 3 ELSE 0 END) AS relevance
FROM Category INNER JOIN ImageBelongsToCategory ON Category.CategoryID = ImageBelongsToCategory.CategoryID INNER JOIN Image ON ImageBelongsToCategory.WIID = Image.WIID
WHERE Title LIKE '%deer%' GROUP BY Image.WIID, Title
ORDER BY relevance DESC