ContainsTable中的通配符全文搜索

时间:2015-11-20 04:29:48

标签: sql-server

我有一个问题,在“包含”中“blah”和“blah *”之间有什么不同? 以下是我的背景。

create table testWildcard(pk int identity constraint twpk primary key, displayName varchar(100))
GO
create fulltext catalog test12 as default
GO
create fulltext index on testWildcard(displayName) key index twpk
GO
insert into testWildcard(displayName) values('blahBlahBlah')
insert into testWildcard(displayName) values('blah<bold>Blah</bold>Blah')
insert into testWildcard(displayName) values('blah Blah Blah')
GO

当使用“blah *”作为搜索关键词时,结果是3条记录。

select * from testWildcard t 
inner join CONTAINSTABLE(testWildcard, *, '"blah*"', LANGUAGE 1033) as w on t.pk=w.[Key]

    results:
            pk  displayName                  Key RANK 
            1   blahBlahBlah                 1  16
            2   blah<bold>Blah</bold>Blah    2  48
            3   blah Blah Blah               3  48

当使用“blah”作为搜索关键字时,结果是2条记录。

select * from testWildcard t 
inner join CONTAINSTABLE(testWildcard, *, '"blah"', LANGUAGE 1033) as w on t.pk=w.[Key]

resuts:
            pk  displayName                  Key RANK 
            2   blah<bold>Blah</bold>Blah    2  48
            3   blah Blah Blah               3  48

然后我使用系统函数sys.dm_fts_parser来分析关键字,它们没有任何区别。我谷歌但我仍然找不到合适的答案。

SELECT * FROM sys.dm_fts_parser ('"blah"', 1033, 0, 0) 
SELECT * FROM sys.dm_fts_parser ('"blah*"', 1033, 0, 0)  
result:
keyword             group_id    phrase_id  occurrence   special_term  display_term expansion_type source_term
 0x0062006C00610068 1           0          1            Exact Match        blah       0             blah

谢谢。

2 个答案:

答案 0 :(得分:2)

FTS中的Star(*)用于外卡与普通查询相同的概念。

因此,当您搜索blah时,会搜索blah附带的字词。 当您使用blah*时,它会搜索以blah开头的单词,无论之后写的是什么。

如果您使用*blah进行搜索,则会搜索结束字blah的位置。

现在你的问题是为什么blahBlahBlah条目没有出现在第一次搜索中,原因是msdn它不是任何一个:(意味着FTS只搜索简单术语中的已知单词,而不是我们创造的任何一个词)

  

CONTAINS可以搜索:

A word or phrase.

The prefix of a word or phrase.

A word near another word.

A word inflectionally generated from another (for example, the word drive is the inflectional stem of drives, drove, driving, and driven).

A word that is a synonym of another word using a thesaurus (for example, the word "metal" can have synonyms such as "aluminum" and "steel").

https://technet.microsoft.com/en-us/library/cc879300%28v=sql.105%29.aspx

How do you get leading wildcard full-text searches to work in SQL Server?

答案 1 :(得分:0)

如果使用sys.dm_fts_index_keywords

查看索引的内容列表
SELECT display_term, column_id, document_count
FROM sys.dm_fts_index_keywords(DB_ID('DatabaseName'), OBJECT_ID('testWildcard'))

你会看到ркууwords:blah,blahblahblah,bold。

这意味着空格<>在创建全文索引时“打破”一个单词。

现在,当您通过“blah”CONTAINS进行搜索时,只返回包含单词blah的字符串。
当您添加通配符*时,搜索“blah *”CONTAINS会返回包含单词的所有字符串,从blah开始。