用于短语排除停止词的FORMSOF的SQL Server全文搜索条件

时间:2012-01-24 09:41:43

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

我想用停用词搜索一些短语,例如“Line Through Crack”。 “通过”是一句话。我希望获得与查询相同的结果

CONTAINS(*, 'FORMSOF(INFLECTIONAL, "Line") AND FORMSOF(INFLECTIONAL, "Crack")')

所有包含除停止词之外的所有单词形式的所有行。 如果客户不知道停用词列表,我可以这样做吗?

1 个答案:

答案 0 :(得分:3)

您使用的是哪个版本的SQL Server?如果它是2008或更高版本,那么您可以在查询运行时以编程方式检索停用词列表。然后,您可以检查是否有任何搜索词位于停用词列表中,并将其从“CONTAINS”查询字符串中排除。

以下查询将返回一个停用词列表(对于美国英语,语言ID为1033):

-- Run the following to get a list of languages and their IDs
select lcid, name from sys.syslanguages order by 1

-- Then use that ID to get a list of stop words
select * from sys.fulltext_stopwords where language_id = 1033

有了这些信息,你可以写一个搜索过程来做这样的事情(这是一个非常基本的例子,但你应该明白这一点):

USE [AdventureWorks]
GO
-- Make sure you have a full-text catalogue to test against
/*
IF EXISTS(SELECT * FROM sys.fulltext_indexes WHERE [object_id] = OBJECT_ID('Production.ProductDescription'))
    DROP FULLTEXT INDEX ON Production.ProductDescription;
IF EXISTS(SELECT * FROM sys.fulltext_catalogs WHERE name = 'FTC_product_description')
    DROP FULLTEXT CATALOG FTC_product_description;
CREATE FULLTEXT CATALOG [FTC_product_description]
    WITH ACCENT_SENSITIVITY = OFF
    AS DEFAULT AUTHORIZATION [dbo]
CREATE FULLTEXT INDEX ON [Production].[ProductDescription]([Description] LANGUAGE [English])
    KEY INDEX [PK_ProductDescription_ProductDescriptionID] ON ([FTC_product_description], FILEGROUP [PRIMARY])
    WITH (CHANGE_TRACKING = AUTO, STOPLIST = SYSTEM);
*/
GO
IF OBJECT_ID('dbo.my_search_proc') IS NULL EXEC ('CREATE PROC dbo.my_search_proc AS ');
GO
-- My Search Proc
ALTER PROC dbo.my_search_proc (
    @query_string   NVARCHAR(1000),
    @language_id    INT = 1033 -- change this to whatever your default language ID is
) AS
BEGIN
    SET NOCOUNT ON;

    ------------------------------------------------------
    -- Split the string into 1 row per word
    ------------------------------------------------------
    -- I've done this in-line here for simplicity, but I 
    -- would recommend creating a CLR function instead
    -- for performance reasons.
    DECLARE @words TABLE (id INT IDENTITY(1,1), word NVARCHAR(100));
    DECLARE @cnt INT, @split_on CHAR(1)
    SELECT @cnt = 1, @split_on = ' ';
    WHILE (CHARINDEX(@split_on, @query_string) > 0) 
    BEGIN 
        INSERT INTO @words (word) 
        SELECT word = LEFT(LTRIM(RTRIM(SUBSTRING(@query_string,1,CHARINDEX(@split_on,@query_string)-1))), 100); 
        SET @query_string = SUBSTRING(@query_string,CHARINDEX(@split_on,@query_string)+1,LEN(@query_string)); 
        SET @cnt = @cnt + 1; 
    END 
    INSERT INTO @words (word)
    SELECT word = LEFT(LTRIM(RTRIM(@query_string)), 100); 

    ------------------------------------------------------
    -- Now build your "FORMSOF" string, excluding stop words.
    ------------------------------------------------------
    DECLARE @formsof NVARCHAR(4000);

    SELECT  @formsof = ISNULL(@formsof, '') 
            + 'FORMSOF(INFLECTIONAL, "' + w.word + '") AND '
    FROM    @words AS w 
    LEFT    JOIN sys.fulltext_system_stopwords AS sw -- use sys.fulltext_stopwords instead if you're using a user-defined stop-word list (or use both)
            ON  w.word = sw.stopword COLLATE database_default 
            AND sw.language_id = @language_id 
    WHERE   sw.stopword IS NULL
    ORDER   BY w.id; -- retain original order in case you do any weighting based on position, etc.

    -- If nothing was returned, then the whole query string was made up of stop-words, 
    -- so just return an empty result set to the application.
    IF @@ROWCOUNT = 0
        SELECT TOP(0) * FROM Production.ProductDescription;

    SET @formsof = LEFT(@formsof, LEN(@formsof)-4); -- Remove the last "AND"
    PRINT 'Query String: ' + @formsof

    ------------------------------------------------------
    -- Now perform the actual Full-Text search
    ------------------------------------------------------
    SELECT  * 
    FROM    Production.ProductDescription
    WHERE   CONTAINS(*, @formsof);
END
GO

EXEC dbo.my_search_proc 'bars for downhill';

所以,如果你搜索“下坡的栏”,那么“for”将被删除(因为它是一个停止词),你应该留下FORMSOF(INFLECTIONAL, "bars") AND FORMSOF(INFLECTIONAL, "downhill").

不幸的是,如果您使用的是SQL 2005并且不知道干扰词文件中的内容,那么您可以做的事情并不多(据我所知)。

干杯, 戴夫