FORMSOF可以嵌套吗?

时间:2012-02-27 13:37:05

标签: sql-server full-text-search formsof

我正在尝试查看SQL Server的全文搜索功能是否可以某种方式使用。

我的想法是做一些事情:拿N个关键词并搜索所述关键词及其同义词的所有形式。

我想出的是这些内容:

SELECT * 
FROM table
WHERE CONTAINS(*, CONTAINS(*,'FORMSOF(THESAURUS,FORMSOF(INFLECTIONAL,"keyword"))')

但是这个查询不起作用。 这甚至可能吗? 我的想法合理,还是我疯了?

2 个答案:

答案 0 :(得分:1)

我认为你必须做这样的事情:

Select * from TABLE
Where 
  CONTAINS(*,'FORMSOF(THESAURUS,"keyword")')
  OR
  CONTAINS(*,'FORMSOF(INFLECTIONAL,"keyword")')

编辑

否则,您可能需要动态构建CONTAINS搜索条件,如下所示:

IF OBJECT_ID('tempdb..#A') IS NOT NULL DROP TABLE #A
IF OBJECT_ID('tempdb..#B') IS NOT NULL DROP TABLE #B

CREATE TABLE #A (pk INT IDENTITY(1,1), word nvarchar(50))
CREATE TABLE #B (word nvarchar(50))

-- load words from thesaurus
INSERT INTO #A (word)
  SELECT display_term 
  FROM sys.dm_fts_parser('FORMSOF (THESAURUS, "mykeyword")',1033,null,0)

DECLARE @cmd NVARCHAR(1000) , @word NVARCHAR(50);
DECLARE @k INT;SET @k = ISNULL((SELECT MAX(pk) FROM #A) , 0 )

-- call INFLECTIONAL for each word
WHILE @k>0
BEGIN
  SELECT @word = word FROM #A WHERE pk = @k
  SET @cmd = 'INSERT INTO #B (word)
    SELECT display_term 
    FROM sys.dm_fts_parser(''FORMSOF (INFLECTIONAL, "' + @word + '")'',1033,null,0)'
  EXEC(@cmd)
  SET @k = @k - 1
END

-- list all words
SELECT * FROM #B

DECLARE @FTString  NVARCHAR(4000);
SET @FTString  = '';

-- build the list of words for CONTAINS
SELECT @FTString  = 
    @FTString + 
  + CASE WHEN @FTString='' 
     THEN '' 
     ELSE ' OR ' 
    END
  + '"'+word+'"'
FROM #B

-- display the CONTAINS condition
SELECT @FTString

-- final search
SELECT * 
FROM dbo.MyTable AS x 
WHERE CONTAINS(*, @FTString)

答案 1 :(得分:0)

值得尝试FREETEXT(...)而不是CONTAINS(...)http://arcanecode.com/2008/05/27/using-formsof-in-sql-server-full-text-searching/