我有一个有效的FREETEXTTABLE查询,用于搜索@searchString。我现在需要使用另一个尝试将@searchString解析为INT的简单查询来UNION,如果成功,则通过查找PK等于解析@searchString的行来过滤表。
以前,我可以轻松地将FREETEXTTABLE结果加入到它正在搜索的表中,按Rank排序,但只选择原始表的已搜索列。
现在,我正在将文本搜索查询和查询行之间的唯一结果与搜索字符串作为关键字进行组合,我不再能够从文本搜索查询中访问Rank。
如何按全文搜索的排名维护排序,但是在全文搜索结果之前,查询结果是否包含主键(如果有结果)?
答案 0 :(得分:0)
您是否尝试过向工会添加一个常量,使您的PK完全匹配在列表顶部?我不记得我的头顶上的自由文本RANK列返回的内容(0到1000我认为),但是这样的事情可以假设你只是使你的常数高于等级的顶部。
DECLARE @id int
IF ISNUMERIC(@myStringId) = 1
SET @id = CAST(@myStringId AS int)
ELSE
SET @id = 0
WITH MyFreetextCte as (SELECT [Rank],
[Key]
FROM FREETEXTTABLE(...)
UNION
SELECT 1001,
(SELECT MyBaseTable.PK FROM MyBaseTable WHERE PK = @id))
SELECT *
FROM MyFreetextCte JOIN MyBaseTable ON MyFreetextCte.[Key] = MyBaseTable.PK
ORDER BY MyFreetextCte.Rank DESC
答案 1 :(得分:0)
在斯科特的大力帮助下,这就是我最终的作品:
CREATE PROCEDURE dbo.testProcedure
(
@searchPhrase nvarchar(500)
)
AS
DECLARE @id int
SET @id = 0;
BEGIN TRY
SET @id = CAST(@id AS int)
END TRY
BEGIN CATCH
END CATCH;
-- at this point, @id will be the primary key if it is only digits
-- otherwise it will default to zero (which is out of range of my identity PK)
WITH ftsTable AS (
SELECT RANK, [KEY] FROM FREETEXTTABLE(sourceTable, *, @searchPhrase)
UNION
SELECT 1001, (SELECT sourceTableID FROM sourceTable WHERE sourceTableID = @id)
)
SELECT sourceTable.*
FROM ftsTable JOIN sourceTable ON ftsTable.[KEY] = sourceTable.sourceTableID
ORDER BY ftsTable.RANK DESC