对几个字符串的全文搜索结果进行排序

ranking full text search results of several strings

我正在从前端获取一些字符串,我想在全文索引视图中找到与尽可能多的这些字符串匹配的所有条目。示例字符串为:

Bla di bladi

这可以翻译成这个查询:

SELECT 
    *
FROM [Schema].[SomeFullTextIndexedView] 
WHERE CONTAINS (*, '"*bla*" OR "*di*" OR "*bladi*"')

效果很好。让我们说,为了论证查询 returns 这些结果:

Column1 Column2 Column3
bla  rte     
bla di   xxx
bladi    tttytyt     
bla di  bladi

我还想引入某种排名,匹配的字符串越多排名越高。然后使用排名对结果进行降序排序:

Column1 Column2 Column3 Rank
bla di  bladi   3
bla di   xxx    2
bla  rte        1
bladi    tttytyt     

1

全文搜索中有什么我可以利用的吗?谢谢

一般来说,可以使用FREETEXTTABLE那个returnsRANK列:

SELECT 
    *
FROM [Schema].[SomeFullTextIndexedView]  AS t 
INNER JOIN FREETEXTTABLE([Schema].[SomeFullTextIndexedView] , *, '"*bla*" OR "*di*" OR "*bladi*"') as k ON t.Id = k.[key]
ORDER BY k.[RANK] DESC

但在您的示例中,文本字符串不相关并且 always returns 0。在真实数据上试用。

您可以使用 CONTAINSTABLE

The table produced by CONTAINSTABLE includes a column named RANK. The RANK column is a value (from 0 through 1000) for each row indicating how well a row matched the selection criteria.

SELECT  *
FROM [Schema].[SomeFullTextIndexedView] t 
INNER JOIN CONTAINSTABLE([Schema].[SomeFullTextIndexedView], ColumnToSearch, '"*bla*" OR "*di*" OR "*bladi*"') c
    ON t.ID = c.[KEY]
ORDER BY [RANK] DESC