Postgres to_tsvector VS :: tsvector

Chr*_*ice 7 postgresql

我真的不明白Postgres to_tsvector和之间的区别::tsvector.我在to_tsvector这里阅读了文档,但似乎没有任何其他文档,::tsvector- 这有点问题.这里提到它,但它在查询之前说了一些关于规范化的事情,并且通过规范化完成了to_tsvector.

我创建了这个SQL Fiddle来演示这两个; 如果您不想离开,这里是代码:

DDL:

CREATE TABLE text (
  text_id serial PRIMARY KEY,
  source_text text NOT NULL,
  destination_text text NOT NULL
);
Run Code Online (Sandbox Code Playgroud)

SQL:

-- Throw some stuff in there
INSERT INTO text (source_text, destination_text) VALUES
('Hello', 'Hello Result'),
('With Comma, Query', 'WithComma, Result');

-- Forced to use punctuation in the query to match what is in the vector
SELECT T.source_text, T.destination_text
FROM   text T
WHERE  LOWER(T.source_text)::tsvector @@ LOWER('Hello')::tsquery;

-- Vector free of punctuation, don't include it in the query
SELECT T.source_text, T.destination_text
FROM   text T
WHERE  to_tsvector(LOWER(T.source_text)) @@ LOWER('Comma')::tsquery;

SELECT ts_debug('english', 'Something without a comma');

SELECT ts_debug('english', 'Something, with a comma');
Run Code Online (Sandbox Code Playgroud)

在我看来,to_tsvector将采取文本,剥去标点符号并返回向量.另一方面,::tsvector似乎在向量中包含标点符号,导致需要在查询中使用相同的标点符号.

两者之间有什么实际差异?一个人通常比另一个更受欢迎吗?在什么情况下每个人都喜欢?

gue*_*tli 11

to_tsvector(text) 读取字符串并对字符串进行一些规范化(将语言设置考虑在内).

::tsvector是一个演员.它没有进行任何规范化(并且不关心语言设置).

请参阅:http://www.postgresql.org/docs/current/interactive/functions-textsearch.html

几年前,一个人在python中编写了一个自己的to_tsvector(),因为我对postgres处理它的方式不满意.这让我更有控制力.

要将数据插入到列中,我使用了tsvector强制转换:

'UPDATE myapp_mymodel SET content=%s::tsvector where id=%s', [tsvector, self.id])
Run Code Online (Sandbox Code Playgroud)