无需重新排序即可返回全文搜索结果页面?
我有一个类似于以下内容的查询,用于返回与全文搜索匹配的文章。在生产中,文本[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)
对结果进行排序。有没有办法编写相同的查询而不使用结果?
I have a query similar to the following to return articles that match a full-text search. In production, the text [FULL TEXT SEARCH CRITERIA]
is replaced with the actual full-text search expression.
The query is written to return only one page of results.
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
This works fine except that full-text search returns the results in order of relevance, but ROW_NUMBER() OVER (ORDER BY ArtViews DESC)
resorts the results. Is there any way to write the same query without resorting the results?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来正确的答案是使用
CONTAINSTABLE
,它的工作方式与CONTAINS
类似,但返回一个表而不是布尔WHERE
条件。所以我最终得到了这样的东西。
It looks like the correct answer to this is to use
CONTAINSTABLE
, which works similar toCONTAINS
but returns a table instead of a BooleanWHERE
condition.So I end up with something like this instead.