我不太明白to_tsvector和::tsvector在Postgres中的区别。我阅读了关于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
);和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');在我看来,to_tsvector将接受文本,去掉标点符号并返回向量。另一方面,::tsvector似乎在向量中包含标点符号,因此需要在查询中使用相同的标点符号。
两者之间的实际区别是什么?一个人普遍优先于另一个人吗?在哪种情况下,每个人都是首选的?
发布于 2014-06-04 20:08:17
to_tsvector(text)读取字符串并对字符串执行一些规范化(考虑到语言设置)。
::tsvector是一个演员。它不进行任何规范化(也不关心语言设置)。
请参阅:http://www.postgresql.org/docs/current/interactive/functions-textsearch.html
几年前,一个人用python编写了一个自己的to_tsvector(),因为我对在postgres中处理这个问题的方式并不满意。这给了我更多的控制权。
为了将数据插入列,我使用了To矢量强制转换:
'UPDATE myapp_mymodel SET content=%s::tsvector where id=%s', [tsvector, self.id])https://stackoverflow.com/questions/24045475
复制相似问题