gpt4 book ai didi

使用前缀和干扰词的 SQL Server 全文搜索

转载 作者:行者123 更新时间:2023-12-05 07:58:40 25 4
gpt4 key购买 nike

我不明白为什么后面的 Query1 和 Query2 没有返回相似的结果集。

示例

查询1:

select * from dbo.tFTS_test where contains (*, '"qwe-asd*"')

返回:

Id          Value
----------- ----------

(0 row(s) affected)

查询2:

select * from dbo.tFTS_test where contains (*, '"qwe-asd"')

返回:

Id          Value
----------- ----------
Informational: The full-text search condition contained noise word(s).
1 qwe-asd

(1 row(s) affected)

表格:

select * from dbo.tFTS_test

返回:

Id          Value
----------- ----------
1 qwe-asd

(1 row(s) affected)

这里有一些辅助查询

帮助查询1:

select * from sys.dm_fts_index_keywords_by_document (db_id(), object_id('dbo.tFTS_test'))

返回:

keyword                         display_term  column_id   document_id          occurrence_count
------------------------------- ------------- ----------- -------------------- ----------------
0x007100770065 qwe 2 1 1
0x007100770065002D006100730064 qwe-asd 2 1 1
0xFF END OF FILE 2 1 1

(3 row(s) affected)

辅助查询2:

select p.*
from sys.fulltext_stoplists s
cross apply sys.dm_fts_parser ('"qwe-asd"', 1033, s.stoplist_id, 0) p
where s.name = 'FTS_test_stoplist'

返回:

keyword                         group_id    phrase_id   occurrence  special_term     display_term  expansion_type source_term
------------------------------- ----------- ----------- ----------- ---------------- ------------- -------------- -----------
0x007100770065002D006100730064 1 0 1 Exact Match qwe-asd 0 qwe-asd
0x007100770065 1 0 1 Exact Match qwe 0 qwe-asd
0x006100730064 1 0 2 Noise Word asd 0 qwe-asd

(3 row(s) affected)

帮助查询3:

select p.*
from sys.fulltext_stoplists s
cross apply sys.dm_fts_parser ('"qwe-asd*"', 1033, s.stoplist_id, 0) p
where s.name = 'FTS_test_stoplist'

返回:

keyword                         group_id    phrase_id   occurrence  special_term     display_term  expansion_type source_term
------------------------------- ----------- ----------- ----------- ---------------- ------------- -------------- -----------
0x007100770065002D006100730064 1 0 1 Exact Match qwe-asd 0 qwe-asd
0x007100770065 1 0 1 Exact Match qwe 0 qwe-asd
0x006100730064 1 0 2 Exact Match asd 0 qwe-asd

(3 row(s) affected)

结构如下:

-- ****************************
-- Step 1. Cleanup FTS Structure
-- ****************************

if exists (select 1 from sys.fulltext_indexes where object_id = object_id('dbo.tFTS_test'))
drop fulltext index on dbo.tFTS_test;
go
if exists (select 1 from sys.fulltext_catalogs where name = 'FTS_test')
drop fulltext catalog FTS_test;
go
if exists (select 1 from sys.fulltext_stoplists where name = 'FTS_test_stoplist')
drop fulltext stoplist FTS_test_stoplist;
go
if object_id ('dbo.tFTS_test') is not null
drop table dbo.tFTS_test;
go

-- ****************************
-- Step 2. Create FTS Structure
-- ****************************

create table dbo.tFTS_test (
Id int not null,
Value varchar(100) not null,
constraint [PK_tFTS_test] primary key clustered (Id asc)
);
go
create fulltext stoplist FTS_test_stoplist from system stoplist;
go
alter fulltext stoplist FTS_test_stoplist add 'asd' language 'English';
go
create fulltext catalog FTS_test with accent_sensitivity = off;
go
create fulltext index on dbo.tFTS_test (Value language English) key index PK_tFTS_test on (FTS_test);
go
if not exists (
select 1
from sys.fulltext_indexes i
inner join sys.fulltext_stoplists l on l.stoplist_id = i.stoplist_id
where i.object_id = object_id('dbo.tFTS_test') and l.name = 'FTS_test_stoplist'
)
alter fulltext index on dbo.tFTS_test set stoplist FTS_test_stoplist;
go
insert into dbo.tFTS_test (Id, Value) values (1, 'qwe-asd');
go

附言抱歉问了这么大的问题。

最佳答案

不同之处在于连字符,全文搜索将您的查询字符串视为两个词而不是一个词。此外,由于“asd”是一个干扰词,因此它不会找到它。

当分词器在术语中遇到分词字符时,分词器将该字符解析为空白字符。

断字字符包括以下几种:

  • •$(美元符号)
  • , (逗号)
  • &(与号)
  • #(井号)

当分词器在术语中遇到连字符 (-) 时,分词器会正确解析该术语。但是,全文同义词库组件将连字符连接的字符与连字符本身一起视为空字符。例如,如果原始术语是“知名名人”,则该术语在同义词库文件中显示为“名人”。

这来自 Microsoft 站点,不是同一个问题,但有一个共同的根本原因:

You obtain incorrect results when you run a full-text search query that uses a thesaurus file in SQL Server 2005

关于使用前缀和干扰词的 SQL Server 全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23998754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com