gpt4 book ai didi

postgresql - Postgres to_tsvector VS::tsvector

转载 作者:行者123 更新时间:2023-11-29 11:21:08 25 4
gpt4 key购买 nike

我不太明白 Postgres 中 to_tsvector::tsvector 之间的区别。我阅读了关于 to_tsvector here 的文档,但似乎没有关于另一个的任何文档,::tsvector - 这有点问题。提到here , 但它在查询之前说了一些关于规范化的事情,规范化是通过 to_tsvector 完成的。

我创建了 this 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 似乎在向量中包含标点符号,导致需要在查询中使用相同的标点符号。

两者之间的实际区别是什么?一个通常比另一个更受欢迎吗?各自在什么情况下更受欢迎?

最佳答案

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])

关于postgresql - Postgres to_tsvector VS::tsvector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24045475/

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