SQL Server 2008 FullText搜索查询

时间:2010-09-20 20:22:50

标签: sql sql-server tsql sql-server-2008 full-text-search

我已将全文搜索添加到我的sql server 2008 express数据库,并在单个表中为两列创建了索引目录。所以现在,我必须重写我的一个存储过程,但我不知道从哪里开始。以下是我需要转换的当前SP,以利用全文搜索功能:

ALTER PROCEDURE [dbo].[sp_page_GetPostsBySearchFront]
(
 @Title nvarchar(256), 
 @Content nvarchar(MAX), 
 @startRowIndex INT,
 @maximumRows INT
) 
AS 
BEGIN
SELECT 
  RowNumber, 
  postId, 
  Title, 
  Content, 
  DateCreated, 
  IsPublished, 
  PublishOnDate, 
  Type, 
  MenuName 
FROM 
(
SELECT ROW_NUMBER() OVER (ORDER BY posts.postId DESC) AS RowNumber, 
  posts.postId, 
  posts.Title, 
  posts.Content, 
  posts.DateCreated, 
  posts.IsPublished, 
  posts.PublishOnDate, 
  posts.Type, 
  posts.MenuName 
FROM posts 
GROUP BY 
  posts.postId, 
  posts.Title, 
  posts.Content, 
  posts.DateCreated, 
  posts.IsPublished, 
  posts.PublishOnDate, 
  posts.Type, 
  posts.MenuName 
HAVING (posts.Title LIKE N'%' + @Title + N'%') 
OR (posts.Content LIKE N'%' + @Content + N'%') 
AND (posts.IsPublished = 1) 
AND (posts.PublishOnDate <= GETDATE()) 
) as u
WHERE u.RowNumber > @startRowIndex 
AND u.RowNumber <= (@startRowIndex + @maximumRows) 

END

有人可以解释我如何完成这项任务吗?我是否使用CONTAINS或FREETEXT以及我在哪里添加它。我迷失了吗?谢谢!

2 个答案:

答案 0 :(得分:1)

使用:

WITH cte AS (
    SELECT ROW_NUMBER() OVER (ORDER BY p.postId DESC) AS RowNumber, 
           p.postId, 
           p.Title, 
           p.Content, 
           p.DateCreated, 
           p.IsPublished, 
           p.PublishOnDate, 
           p.Type, 
           p.MenuName 
      FROM POSTS p
     WHERE (   CONTAINS(p.title, @Title)
            OR CONTAINS(p.content, @Content))
       AND p.IsPublished = 1
       AND p.PublishOnDate <= GETDATE() )
 SELECT u.RowNumber, 
        u.postId, 
        u.Title, 
        u.Content, 
        u.DateCreated, 
        u.IsPublished, 
        u.PublishOnDate, 
        u.Type, 
        u.MenuName 
   FROM cte as u
  WHERE u.RowNumber > @startRowIndex 
    AND u.RowNumber <= (@startRowIndex + @maximumRows)

太糟糕了,您有不同的参数来搜索标题和内容字段 - could've been consolidated into a single CONTAINS if the parameters are the same value

documentation provides a nice breakdown of when they suggest Full Text Search (FTS) functionality。大多数示例建议使用CONTAINS / CONTAINSTABLE,并使用FREETEXT / FREETEXTTABLE:

  • 屈折形式是动词的不同时态或名词的单数和复数形式。
  • 同义词库定义用户指定的术语同义词。

答案 1 :(得分:1)

好的,这就是我做的工作。我收到噪音消息的原因是因为我没有正确设置参数值length @search。一旦我意识到并改变它,它就可以正常工作。

ALTER PROCEDURE [dbo].[sp_page_GetPostsByFTS] 
( 
    @search nvarchar(255), 
    @startRowIndex INT, 
    @maximumRows INT 
) 
AS 
BEGIN 
SELECT 
    RowNumber, 
    postId, 
    Title, 
    Content, 
    DateCreated, 
    IsPublished, 
    PublishOnDate, 
    Type, 
    MenuName 
FROM 
( 
    SELECT 
    ROW_NUMBER() OVER (ORDER BY posts.postId DESC) AS RowNumber, 
        posts.postId, 
        posts.Title, 
        posts.Content, 
        posts.DateCreated, 
        posts.IsPublished, 
        posts.PublishOnDate, 
        posts.Type, 
        posts.MenuName 
    FROM posts 
    WHERE FREETEXT((Title,Content),@search) 
    AND (posts.IsPublished = 1) 
    AND (posts.PublishOnDate <= GETDATE()) 
) 
as u 
WHERE u.RowNumber > @startRowIndex 
AND u.RowNumber <= (@startRowIndex + @maximumRows) 
END