喜欢多样化的词,不喜欢重复的词

Favor variety of words, not repeated words

考虑在 SQL 服务器中对这些记录进行全文搜索索引:

ID  Content
1   'All about cars, cars, cars'
2   'My house and my car'
3   'A house is a house is a house'

当用户搜索术语 "house" 和 "car" 时,我希望记录 2 作为最高结果返回,因为这似乎是 he/she 正在寻找的内容。

但是,我找不到实现这个的方法。这些查询在 2 之上排名第 1 和 3,因为其中一个词重复了 3 次。

SELECT * FROM CONTAINSTABLE(TestTable,Content,'ISABOUT(FORMSOF(INFLECTIONAL,"car"),FORMSOF(INFLECTIONAL,"house"))')
SELECT * FROM CONTAINSTABLE(TestTable,Content,'FORMSOF(INFLECTIONAL,"car") OR FORMSOF(INFLECTIONAL,"house")')

有什么方法可以达到预期的效果吗?

不是单个查询,但您可以根据用户的搜索词进行 table,加入内容,但每个搜索词仅一次,GROUP BY 搜索词,然后 select COUNT(*) 最高的 TOP 行。

这不使用全文搜索,但可以得到您想要的结果。

您可以 运行 2 个全文搜索 -- 一个使用 AND(以确保所有单词都匹配),另一个使用 OR。

declare @results table (Key int, Rank int, OrderBy int)

-- get rows that match ALL keywords
INSERT @results
SELECT Key, Rank, 1
FROM CONTAINSTABLE(TestTable,Content,'FORMSOF(INFLECTIONAL,"car") AND FORMSOF(INFLECTIONAL,"house")')

-- get rows that match ANY keyword, ignoring rows already matched in the previous query
INSERT @results
SELECT Key, Rank, 2
FROM CONTAINSTABLE(TestTable,Content,'FORMSOF(INFLECTIONAL,"car") OR FORMSOF(INFLECTIONAL,"house")')
WHERE Key not in (SELECT Key FROM @results)

-- select the AND matches first, followed by the OR matches
SELECT * FROM @results
ORDER BY OrderBy, Rank