简体   繁体   English

具有多个字段排名的 SQL Server 全文搜索

[英]SQL Server Fulltext search with ranking on multiple fields

I'm trying to build a very simple search engine for a library.我正在尝试为图书馆构建一个非常简单的搜索引擎。

I have a table [Tales] that contains the following fulltext indexed columns TITLE , AUTHOR and GENRE .我有一个表 [Tales],其中包含以下全文索引列TITLEAUTHORGENRE

The user can enter a search phrase like this: "american war by david potter"用户可以输入这样的搜索词组:“大卫·波特的美国战争”

I split each word building the following query:我拆分每个单词构建以下查询:

SELECT TOP (100) 
    [title],
    [author],
    [genre],
    [TaleID],
    k.[RANK]
FROM 
    [Books].[dbo].[Tales] AS t 
INNER JOIN 
    CONTAINSTABLE([Tales] , *, '"history" OR "america" OR "war" OR "by" OR "david" OR "potter"') AS k ON t.TaleID = k.[key]
ORDER BY 
    k.[RANK] DESC

and the result is the following:结果如下:

在此处输入图片说明

The highlighted line, row No.28, should be the one with the higher ranking, as it contains all the words searched by the user.突出显示的第 28 行应该是排名较高的行,因为它包含用户搜索的所有单词。 Instead it seems that the fulltext engine prefer the multiple occurrences of the same word "american".相反,全文引擎似乎更喜欢同一个词“美国人”的多次出现。

What's wrong with my query?我的查询有什么问题?

EDIT : trying with FREETEXTTABLE , the results are the same.编辑:尝试使用FREETEXTTABLE ,结果是一样的。 But adding more terms I can get the right result on top:但是添加更多术语我可以得到正确的结果:

SELECT TOP (1000) 
    [title],
    [author],
    [genre],
    [publisher],
    [storyteller],
    [TaleID],
    k.[RANK]
FROM 
    [Books].[dbo].[Tales] AS t 
INNER JOIN 
    FREETEXTTABLE([Tales], ([title],[author],[genre]), 'history of america''s civil war by david potter') as k ON t.TaleID = k.[key]
ORDER BY
    k.[RANK] DESC;

Here is a link to the documentation of the ranking algorithms and Query with Full-Text Search 这是排名算法和全文搜索查询的文档链接

From the documentation: 从文档中:

CONTAINS/CONTAINSTABLE 中包含/ CONTAINSTABLE
Match single words and phrases with precise or fuzzy (less precise) matching. 使用精确或模糊(不太精确)匹配来匹配单个单词和短语。 You can also do the following things: Specify the proximity of words within a certain distance of one another. 您还可以执行以下操作:指定单词之间在一定距离内的接近度。 Return weighted matches. 返回加权匹配。 Combine search conditions with logical operators. 将搜索条件与逻辑运算符结合在一起。

FREETEXT/FREETEXTTABLE FREETEXT / FREETEXTTABLE
Match the meaning, but not the exact wording, of specified words, phrases, or sentences (the freetext string). 匹配指定单词,短语或句子(自由文本字符串)的含义,但不匹配确切的措辞。 Matches are generated if any term or form of any term is found in the full-text index of a specified column. 如果在指定列的全文索引中找到任何术语或任何术语的形式,则将生成匹配项。

Try this...尝试这个...

Declare @text varchar(255)

SELECT distinct coalesce(s1.rank, 0) + coalesce(s2.rank, 0) + coalesce(s3.rank, 0) as TotalRank, [title], [author], [genre], [TaleID], k.[RANK] FROM [Books].[dbo].[Tales] AS t选择不同的合并(s1.rank,0)+合并(s2.rank,0)+合并(s3.rank,0)作为总排名,[标题],[作者],[流派],[故事ID],k.[排名] 从 [书籍].[dbo].[故事] 作为 t

inner join freetexttable(dbo.Tales, Title, @text) s1 on s.id = s1.[Key]
inner join freetexttable(dbo.Tales, Author, @text) s2 on s.id = s2.[Key]
inner join freetexttable(dbo.Tales, Genre, @text) s3 on s.id = s3.[Key]

where (coalesce(s1.rank, 0) + coalesce(s2.rank, 0) + coalesce(s3.rank, 0)) > 0

order by TotalRank desc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM