首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Postgres to_tsvector VS ::tsvector向量

Postgres to_tsvector VS ::tsvector向量
EN

Stack Overflow用户
提问于 2014-06-04 18:59:56
回答 1查看 15.2K关注 0票数 11

我不太明白to_tsvector::tsvector在Postgres中的区别。我阅读了关于to_tsvector这里的文档,但似乎没有其他文档,::tsvector --这有点问题。它提到了这里,但是它提到了在查询之前进行规范化,并且规范化是通过to_tsvector完成的。

我创建这个SQL Fiddle是为了演示这两者;如果您不想导航,下面是代码:

DDL

代码语言:javascript
复制
CREATE TABLE text (
  text_id serial PRIMARY KEY,
  source_text text NOT NULL,
  destination_text text NOT NULL
);

SQL

代码语言:javascript
复制
-- 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似乎在向量中包含标点符号,因此需要在查询中使用相同的标点符号。

两者之间的实际区别是什么?一个人普遍优先于另一个人吗?在哪种情况下,每个人都是首选的?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 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矢量强制转换:

代码语言:javascript
复制
'UPDATE myapp_mymodel SET content=%s::tsvector where id=%s', [tsvector, self.id])
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24045475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档