PostgreSQL 全文搜索 (FTS),::tsvector 和 to_tsvector() 有什么区别?

vor*_*bey 2 postgresql full-text-search cast

我有一个带有 tsvector 类型列(col)的表。

我通过脚本填充了一些行并使用 cast data::tsvector,但是(!)有些行填充了to_tsvector(data),所以我有具有不同值的行,如下所示:

  1. '?' '?' '???' '??????' '???????' '???????????' '???????????' '??????????'

  2. '?':7 '??':8 '?????':5 '?????':1 '?????????':3 '?????????':2 '????????':4

这对我来说很惊讶 :) 那么没有用to_tsvector(data)准备的值的开销是多少?pg 会在运行时做 to_tsvector 还是什么?

Eva*_*oll 6

string->tsvector强制转换和函数构造器之间的区别to_tsvector(text)

  1. cast ( ::) 假定输入已经是一个已转换为文本的 tsvector。它使用特定于语言的存根机制或存储位置信息,它只是假设它被赋予了一个以空格分隔的文本搜索标记(位置和词素),并假设它们已经经历了这个过程。
  2. to_tsvector() 接受字符串输入并做所有必要的工作来标记它,包括规范化和存根。

除了 tsvector 的文本表示之外,您可能应该避免对所有内容进行强制转换。

例子

在这里,我们将字符串 'test testing testing' 读作三个 tsvector 词素,位置未定义,注意输出缺少存根

test=# select 'test testing tested'::tsvector;
         tsvector          
---------------------------
 'test' 'tested' 'testing'
(1 row)
Run Code Online (Sandbox Code Playgroud)

在这里,我们通过在您的特定 中指定的过程TEXT SEARCH CONFIGURATION(在我的情况下为english.

test=# SELECT to_tsvector('test testing tested');
 to_tsvector  
--------------
 'test':1,2,3
(1 row)
Run Code Online (Sandbox Code Playgroud)

这里我们明确告诉 PostgreSQL 使用simple文本搜索配置。

test=# SELECT to_tsvector('simple', 'test testing tested');
           to_tsvector           
---------------------------------
 'test':1 'tested':3 'testing':2
(1 row)
Run Code Online (Sandbox Code Playgroud)

那么演员表是干什么用的呢?但是,您可以将它用于类似的事情

SELECT $$'test':1 'tested':3 'testing':2$$::tsvector @@ 'testing';
Run Code Online (Sandbox Code Playgroud)

这是另一个案例,展示了它的作用..(返回真)

SELECT to_tsvector(test) = to_tsvector(test)::text::tsvector
FROM ( VALUES
  ( 'This is my test. Hello from testing test tested' )
) AS t(test);
Run Code Online (Sandbox Code Playgroud)

如果您通过网络获取 tsvector,或者在 的情况下将其序列化为文本pg_dump,这可能很有用。