我有一个类似于以下内容的查询,以返回与全文搜索匹配的文章。在生产中,文本[FULL TEXT SEARCH CRITERIA]
将替换为实际的全文搜索表达式。
编写查询只返回一页结果。
WITH ArtTemp AS (
SELECT TOP (@StartRow + @MaxRows) ROW_NUMBER() OVER (ORDER BY ArtViews DESC) AS RowID,
Article.ArtID,Article.ArtTitle,Article.ArtSlug,Category.CatID,Category.CatTitle,
Article.ArtDescription,Article.ArtCreated,Article.ArtUpdated,Article.ArtUserID,
[User].UsrDisplayName AS UserName FROM Article
INNER JOIN Subcategory ON Article.ArtSubcategoryID = Subcategory.SubID
INNER JOIN Category ON Subcategory.SubCatID = Category.CatID
INNER JOIN [User] ON Article.ArtUserID = [User].UsrID
WHERE [FULL TEXT SEARCH CRITERIA] AND Article.ArtApproved = 1
)
SELECT ArtID,ArtTitle,ArtSlug,CatID,CatTitle,ArtDescription,ArtCreated,ArtUpdated,
ArtUserID,UserName FROM ArtTemp
WHERE RowID BETWEEN @StartRow + 1 AND (@StartRow + @MaxRows)
ORDER BY RowID
此工作正常,但全文搜索按相关性顺序返回结果,但ROW_NUMBER() OVER (ORDER BY ArtViews DESC)
会对结果进行调整。有没有办法在不诉诸结果的情况下编写相同的查询?
答案 0 :(得分:1)
对此的正确答案似乎是使用CONTAINSTABLE
,其工作方式类似于CONTAINS
但返回表而不是布尔WHERE
条件。
所以我最终会得到类似的东西。
WITH ArtTemp AS (
SELECT TOP (@StartRow + @MaxRows) ROW_NUMBER() OVER (ORDER BY FTS.RANK DESC) AS RowID,
Article.ArtID,Article.ArtTitle,Article.ArtSlug,Category.CatID,Category.CatTitle,
Article.ArtDescription,Article.ArtCreated,Article.ArtUpdated,Article.ArtUserID,
[User].UsrDisplayName AS UserName FROM Article
INNER JOIN Subcategory ON Article.ArtSubcategoryID = Subcategory.SubID
INNER JOIN Category ON Subcategory.SubCatID = Category.CatID
INNER JOIN [User] ON Article.ArtUserID = [User].UsrID
INNER JOIN CONTAINSTABLE(Article, *, 'FORMSOF(INFLECTIONAL, abc)') AS FTS ON Article.ArtID = FTS.[KEY]
WHERE Article.ArtApproved = 1
)
SELECT ArtID,ArtTitle,ArtSlug,CatID,CatTitle,ArtDescription,ArtCreated,ArtUpdated,
ArtUserID,UserName FROM ArtTemp
WHERE RowID BETWEEN @StartRow + 1 AND (@StartRow + @MaxRows)
ORDER BY RowID