gpt4 book ai didi

sql - 使用 "to_tsquery"和 "simple"时,tsvector 上的 "english"会产生不同的结果?

转载 作者:行者123 更新时间:2023-11-29 11:13:03 24 4
gpt4 key购买 nike

我应征入伍帮助完成一个项目,在多年未使用 PostgreSQL 后,我又重新投入使用。除了缺乏使用之外,我以前从未遇到过使用 tsvector 字段,现在发现自己面临基于它们的错误。我阅读了有关字段类型及其用途的文档,但我很难挖掘有关“简单”与“英语”作为 to_tsquery() 的第一个参数有何不同的文档

例子

> SELECT to_tsvector('mortgag') @@ to_tsquery('simple', 'mortgage')
?column?
----------
f
(1 row)

> SELECT to_tsvector('mortgag') @@ to_tsquery('english', 'mortgage')
?column?
----------
t
(1 row)

我认为它们都应该返回 true,但显然第一个不返回 - 为什么?

最佳答案

FTS 利用 dictionaries规范化文本:

12.6. Dictionaries

Dictionaries are used to eliminate words that should not be considered in a search (stop words), and to normalize words so that different derived forms of the same word will match. A successfully normalized word is called a lexeme.

因此,字典用于排除在搜索中考虑的过于常见或无意义的事物(停用词),并规范化其他一切,因此城市cities,例如,即使它们是不同的词也会匹配。

让我们看看 ts_debug 的一些输出看看字典发生了什么:

=> select * from ts_debug('english', 'mortgage');
alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+----------+----------------+--------------+-----------
asciiword | Word, all ASCII | mortgage | {english_stem} | english_stem | {mortgag}

=> select * from ts_debug('simple', 'mortgage');
alias | description | token | dictionaries | dictionary | lexemes
-----------+-----------------+----------+--------------+------------+------------
asciiword | Word, all ASCII | mortgage | {simple} | simple | {mortgage}

请注意,simple 使用 simple 字典,而 english 使用 english_stem 字典。

simple dictionary :

operates by converting the input token to lower case and checking it against a file of stop words. If it is found in the file then an empty array is returned, causing the token to be discarded. If not, the lower-cased form of the word is returned as the normalized lexeme.

simple 字典只会抛出停用词、小写,仅此而已。我们可以亲眼看到它的简洁:

=> select to_tsquery('simple', 'Mortgage'), to_tsquery('simple', 'Mortgages');
to_tsquery | to_tsquery
------------+-------------
'mortgage' | 'mortgages'

simple 字典太简单了,甚至无法处理简单的复数。

那么这本 english_stem 字典是关于什么的呢? “词干”后缀是一个赠品:这本词典将词干提取算法应用于单词,以将(例如)citycities 转换为同一事物。来自fine manual :

12.6.6. Snowball Dictionary

The Snowball dictionary template is based on a project by Martin Porter, inventor of the popular Porter's stemming algorithm for the English language. [...] Each algorithm understands how to reduce common variant forms of words to a base, or stem, spelling within its language.

在它的正下方,我们看到了 english_stem 字典:

CREATE TEXT SEARCH DICTIONARY english_stem (
TEMPLATE = snowball,
Language = english,
StopWords = english
);

因此 english_stem 词典提取词干,我们可以看到这种情况发生了:

=> select to_tsquery('english', 'Mortgage'), to_tsquery('english', 'Mortgages');
to_tsquery | to_tsquery
------------+------------
'mortgag' | 'mortgag'

执行摘要:'simple' 表示简单的文字匹配,'english' 应用词干提取(希望)产生更好的匹配。词干化将 mortgage 变成了 mortgag,这给了你你的比赛。

关于sql - 使用 "to_tsquery"和 "simple"时,tsvector 上的 "english"会产生不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10747395/

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