在SQL Server中使用全文搜索的奇怪行为

前端之家收集整理的这篇文章主要介绍了在SQL Server中使用全文搜索的奇怪行为前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有列消息NVARCHAR(MAX)的MyTable.

ID为1的记录包含消息’0123456789333444 Test’

当我运行以下查询

DECLARE @Keyword NVARCHAR(100)

SET @Keyword = '0123456789000001*'

SELECT *
FROM MyTable
WHERE CONTAINS(Message,@Keyword)

记录ID 1显示在结果中,在我看来它不应该因为0123456789333444不包含0123456789000001.

有人可以解释为什么记录会出现吗?

编辑

select * from sys.dm_fts_parser('"0123456789333444 Test"',1033,0)

返回以下内容

group_id phrase_id occurrence special_term  display_term        expansion_type source_term
1        0         1           Exact Match  0123456789333444    0              0123456789333444 Test
1        0         1           Exact Match  nn0123456789333444  0              0123456789333444 Test
1        0         2           Exact Match  test                0              0123456789333444 Test

解决方法

这是因为@Keyword没有用双引号括起来.这会强制零,一个或多个匹配.

Specifies a match of words or phrases beginning with
the specified text. Enclose a prefix term in double quotation marks
(“”) and add an asterisk () before the ending quotation mark,so that
all text starting with the simple term specified before the asterisk
is matched. The clause should be specified this way: CONTAINS (column,
‘”text
“‘). The asterisk matches zero,one,or more characters (of the
root word or words in the word or phrase). If the text and asterisk
are not delimited by double quotation marks,so the predicate reads
CONTAINS (column,‘text*’),full-text search considers the asterisk as
a character and searches for exact matches to text*. The full-text
engine will not find words with the asterisk (*) character because
word breakers typically ignore such characters.

When is a phrase,each word contained in the phrase is
considered to be a separate prefix. Therefore,a query specifying a
prefix term of “local wine*” matches any rows with the text of “local
winery”,“locally wined and dined”,and so on.

看看关于这个主题的MSDN. MSDN

猜你在找的MsSQL相关文章