这是旧的,用 VB 编写的,但我认为这个原理对你有用。
此代码并不完全限于具有所有标签的图片,而是将标签点击次数最多的图片推到顶部。
此代码的工作原理是首先向用户展示最适合的内容,然后在下面为他们提供其他选项。你的 AND 场景会削减对 5 个标签中的 4 个有效的东西。在这种情况下,他们只会在标签下方显示 5 分中的 5 分。
因此,如果您有一些带有以下标签的图像:
鉴于:
Image1 女人、狗、熊猫
Image2 女人,电话,微笑
Image3 人、狗、熊猫、香蕉
Image4 男人,电话,微笑
产量:
“电话微笑”的标签搜索会将 [Image2] 和 [Image4] 评分到列表顶部。
“熊猫”的标签搜索只会产生 Image1 和 Image3。
“man dog pandabanana”的标签搜索将产生 Image3 作为顶部,然后是 Image1,然后是 Image4。
这个实现是为了根据标签找到最好的图像。
您在自己的 SQL 数据库中创建 3 个表(如果您正在做网页或故事或为了您自己的清晰而更改图像):
SQL 表:
imageTag [INT ID, String Tag]
imageImages [INT ID, NVARCHAR(2000) Path]
imageConnections [INT TagID, INT ImageID]
VB.NET 代码:
'The beef of the SQL statement to get the scored results is here.
Dim SearchString As String =
"SELECT it.path, count(it.path) AS cnt, it.Id, it.name, it.description, it.updated FROM imageimages AS it, imageconnections AS itt, imagetags AS t WHERE " + _
"{REPLACE-TAG} AND t.id = itt.tagid AND it.id = itt.imageid " + _
"GROUP BY Path, it.ID, it.name, it.description, it.updated ORDER BY cnt DESC"
Dim keywords As String() = tstSearch.Text.Split(New String() {" "}, StringSplitOptions.RemoveEmptyEntries)
If keywords.Length > 0 Then
Dim strReplacement As String
strReplacement = "( tag = '" + keywords(0) + "'"
If keywords.Length > 1 Then
For i As Integer = 1 To keywords.Length - 1
strReplacement += " OR tag = '" + keywords(i) + "'"
Next
End If
strReplacement += " )"
SearchString = SearchString.Replace("{REPLACE-TAG}", strReplacement)
dt = tran.GetDataTable(SearchString)
End If