gpt4 book ai didi

sql - 如何进行多关键词搜索?

转载 作者:行者123 更新时间:2023-12-02 09:01:57 25 4
gpt4 key购买 nike

我有 2 个表要搜索。搜索照片的关键字、标题和描述。关键字已被拆分到一个单独的表中。我的高级搜索将允许搜索所有 3 个,但基本搜索只是关键字表。

基本表设置:

照片表

  • 照片ID
  • 姓名
  • 标题
  • 描述

WORD2PHOTO表

  • WordID
  • 照片ID
  • 单词

一直在尝试 View 和存储过程,但没有得到任何好的结果。我从 View 中获取照片,但在多个记录中。我会在应用程序端进行过滤和类似的操作,但我使用的是亚音速,并且想使用内置的分页;那是另一个问题了。

首先,如何搜索多个关键字?二、如何添加标题和描述的搜索?

我有一个函数(f_Split),它将返回当前要搜索的单词的临时表,

DECLARE @Words TABLE (Word varchar(20))

INSERT INTO @Words (Word)
SELECT Keyword FROM dbo.f_Split('cars|auto|red|fast','|')

现在如何使用它生成的表来获取照片记录?这几天一直在挣扎吗?感谢您的帮助。

最佳答案

假设您有表 Photo 和表 Word,并且使用附加表 WordToPhoto 建立多对多关系:

DECLARE @Photo TABLE
(ID INT, Name VARCHAR(20), Title VARCHAR(50), Description VARCHAR(200))
INSERT INTO @Photo
SELECT 1, 'mountain.jpg', 'Mountain trip', 'Mountain trip'
UNION
SELECT 2, 'beach.jpg', 'On the beach', 'On the beach'
UNION
SELECT 3, 'garden.jpg', 'Garden', 'Garden'

DECLARE @Word TABLE(ID INT, Value VARCHAR(20))
INSERT INTO @Word
SELECT 1, 'dog'
UNION
SELECT 2, 'flowers'
UNION
SELECT 3, 'sea'
UNION
SELECT 4, 'moon'
UNION
SELECT 5, 'mountain'
UNION
SELECT 6, 'seashell'
UNION
SELECT 7, 'shell'
UNION
SELECT 8, 'concert'

DECLARE @WordToPhoto TABLE(ID INT, PhotoID INT, WordID INT)
INSERT INTO @WordToPhoto
SELECT 1, 1, 2
UNION
SELECT 2, 1, 4
UNION
SELECT 3, 2, 1
UNION
SELECT 4, 2, 3
UNION
SELECT 5, 2, 6
UNION
SELECT 6, 3, 1
UNION
SELECT 7, 3, 2

您对“doggy”和“flower”关键字进行搜索查询:

DECLARE @Words TABLE (Word VARCHAR(20))
INSERT INTO @Words
SELECT 'doggy' UNION
SELECT 'flower'

如果您使用多个关键字进行搜索,那么您可能需要一些评级系统。我的建议是为每张照片总结要点。将使用以下算法计算分数:

If Photo Keyword is the same as Search Keyword then 1 (dog = dog)Else If Photo Keyword is a Search Keyword with postfix then 0.75 (dog LIKE doggy)Else If Search Keyword is a Photo Keyword with postfix then 0.75 (doggy LIKE dog) Else If Photo Keyword is a Search Keyword with prefix then 0.5 (dog LIKE bulldog) Else If Search Keyword is a Photo Keyword with prefix then 0.5 (hotdog LIKE dog) Else If Photo Keyword is a Search Keyword with prefix and postfix then 0.25 (dog LIKE Snoopdogg) Else If Search Keyword is a Photo Keyword with prefix and postfix then 0.25 (overdogged LIKE dog) 

Point coefficients may be other, this is just an example.

Implementation:

SELECT R.Rating, P.* FROM @Photo P
INNER JOIN
(
SELECT PhotoID, SUM(W.Point) AS Rating
FROM @WordToPhoto WTP
INNER JOIN (
SELECT W.ID,
CASE
WHEN (LOWER(WS.Word) = LOWER(W.Value)) THEN 1
WHEN (LOWER(WS.Word) LIKE LOWER(W.Value)+'%')
OR (LOWER(W.Value) LIKE LOWER(WS.Word)+'%') THEN 0.75
WHEN (LOWER(WS.Word) LIKE '%'+LOWER(W.Value))
OR (LOWER(W.Value) LIKE '%'+LOWER(WS.Word)) THEN 0.5
ELSE 0.25
END AS Point
FROM @Word W
INNER JOIN @Words WS ON LOWER(WS.Word) LIKE '%'+LOWER(W.Value)+'%'
OR LOWER(W.Value) LIKE '%'+LOWER(WS.Word)+'%'
) AS W ON W.ID = WTP.WordID
GROUP BY PhotoID
) AS R ON P.ID = R.PhotoID

ORDER BY R.Rating DESC

结果:

Rating ID      Name            Title           Description1.50   3       garden.jpg      Garden          Garden0.75   1       mountain.jpg    Mountain trip   Mountain trip0.75   2       beach.jpg       On the beach    On the beach

关于sql - 如何进行多关键词搜索?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/549601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com