SQL Server全文搜索转义字符? [英] SQL Server Full Text Search Escape Characters?

查看:111
本文介绍了SQL Server全文搜索转义字符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一个MS SQL Server全文搜索查询。我需要转义特殊字符,以便搜索包含特殊字符的特定字词。是否有一个内置函数来转义全文搜索字符串?如果不是,你会怎么做? 解决方案

坏消息:没有办法。好消息:你不需要它(因为它无论如何也无济于事)。



我在其中一个项目中遇到了类似的问题。我的理解是,在构建全文索引时,SQL Server会将所有特殊字符视为单词分隔符,因此:


  1. 字符在全文索引中表示为两个(或多个)字词。

  2. 这些字符被删除并且不会出现在索引中。

考虑我们下面的表格以及相应的全文索引(它被跳过):

  CREATE TABLE [dbo]。[ActicleTable] 

[Id] int identity(1,1)not null primary key,
[ ActicleBody] varchar(max)not null
);

稍后我们将表添加到行:

  INSERT INTO [ActicleTable] values('digitally improvements folders')
INSERT INTO [ActicleTable] values('digitally improve {ments} fold(ers)')

尝试搜索:

  SELECT * FROM [ArticleTable] WHERE CONTAINS(*,'digitally')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*,'improvements')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*,'folders')

  SELECT * FROM [ArticleTable] WHERE CONTAINS(*,'digital')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*,'improve')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*,'fold')

第一组条件匹配第一行(而不是第二行),而第二组匹配第二行。



不幸的是我找不到al墨迹到MSDN(或其他地方),这些行为是明确陈述的。但是我找到了官方文章,它告诉如何将引号全文搜索查询,这与上述算法[隐含地]一致。

I am doing a MS SQL Server Full Text Search query. I need to escape special characters so I can search on a specific term that contains special characters. Is there a built-in function to escape a full text search string ? If not, how would you do it ?

解决方案

Bad news: there's no way. Good news: you don't need it (as it won't help anyway).

I've faced similar issue on one of my projects. My understanding is that while building full-text index, SQL Server treats all special characters as word delimiters and hence:

  1. Your word with such a character is represented as two (or more) words in full-text index.
  2. These character(s) are stripped away and don't appear in an index.

Consider we have the following table with a corresponding full-text index for it (which is skipped):

CREATE TABLE [dbo].[ActicleTable] 
(
  [Id] int identity(1,1) not null primary key,
  [ActicleBody] varchar(max) not null
);

Consider later we add rows to the table:

INSERT INTO [ActicleTable] values ('digitally improvements folders')
INSERT INTO [ActicleTable] values ('digital"ly improve{ments} fold(ers)')

Try searching:

SELECT * FROM [ArticleTable] WHERE CONTAINS(*, 'digitally')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*, 'improvements')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*, 'folders')

and

SELECT * FROM [ArticleTable] WHERE CONTAINS(*, 'digital')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*, 'improve')
SELECT * FROM [ArticleTable] WHERE CONTAINS(*, 'fold')

First group of conditions will match first row (and not the second) while the second group will match second row only.

Unfortunately I could not find a link to MSDN (or something) where such behaviour is clearly stated. But I've found an official article that tells how to convert quotation marks for full-text search queries, which is [implicitly] aligned with the above described algorithm.

这篇关于SQL Server全文搜索转义字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆