Postgres 中的相似函数与 pg_trgm

发布于 2024-08-21 12:47:46 字数 479 浏览 11 评论 0原文

我正在尝试使用 Postgres 中的相似性函数来进行一些模糊文本匹配,但是每当我尝试使用它时,我都会收到错误:

function similarity(character varying, unknown) does not exist

如果我向文本添加显式转换,则会收到错误:

function similarity(text, text) does not exist

我的查询是:

SELECT (similarity("table"."field"::text, %s::text)) AS "similarity", "table".* FROM "table" WHERE similarity > .5 ORDER BY "similarity" DESC LIMIT 10

我需要吗做些什么来初始化 pg_trgm?

I'm trying to use the similarity function in Postgres to do some fuzzy text matching, however whenever I try to use it I get the error:

function similarity(character varying, unknown) does not exist

If I add explicit casts to text I get the error:

function similarity(text, text) does not exist

My query is:

SELECT (similarity("table"."field"::text, %s::text)) AS "similarity", "table".* FROM "table" WHERE similarity > .5 ORDER BY "similarity" DESC LIMIT 10

Do I need to do something to initalize pg_trgm?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

用心笑 2024-08-28 12:47:46

使用 postgresql 9.1:

安装后(在 ubuntu 上)sudo apt-get install postgresql-contrib 正如 tomaszbak 的回答。

你只需要执行sql命令:

CREATE EXTENSION pg_trgm;

With postgresql 9.1:

after installing (on ubuntu) sudo apt-get install postgresql-contrib as tomaszbak answered.

you just have to execute the sql command:

CREATE EXTENSION pg_trgm;
泪意 2024-08-28 12:47:46

你必须安装pg_trgm。在 debian 中,获取此 sql:/usr/share/postgresql/8.4/contrib/pg_trgm.sql。从命令行:

psql -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

或在 psql shell 内:

\i /usr/share/postgresql/8.4/contrib/pg_trgm.sql

脚本默认安装在公共模式中,如果您想将其安装在其他位置,请编辑顶部的搜索路径(这样只需删除模式即可完成卸载/升级) )。

You have to install pg_trgm. In debian, source this sql: /usr/share/postgresql/8.4/contrib/pg_trgm.sql. From the command line:

psql -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

Or inside a psql shell:

\i /usr/share/postgresql/8.4/contrib/pg_trgm.sql

The script defaults to installing in the public schema, edit the search path at the top if you want to install it somewhere else (so that uninstalling/upgrading can be done simply by dropping the schema).

非要怀念 2024-08-28 12:47:46

在 ubuntu 上,您需要运行

sudo apt-get install postgresql-contrib

以获取 /usr/share/postgresql/8.4/contrib/pg_trgm.sql

On ubuntu you need to run

sudo apt-get install postgresql-contrib

to get /usr/share/postgresql/8.4/contrib/pg_trgm.sql

寒尘 2024-08-28 12:47:46

如果您在 public 架构中安装了 pg_trgm 扩展,,则在使用相似性时必须显式指定架构像这样的功能

select schema.similarity(foo,bar) from schema.baz

If you have the pg_trgm extension installed not in the public schema you must explicitly specify the schema when using the similarity function like this

select schema.similarity(foo,bar) from schema.baz
打小就很酷 2024-08-28 12:47:46

对于 Postgres 8.4 执行以下操作:

以 sudo 用户身份运行:

sudo apt-get install postgresql-contrib-8.4

切换到 postgres 用户:

sudo su - postgres >

运行:

psql -U DB_USER -d DB_NAME -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

重新启动 postgres

For Postgres 8.4 do following:

As sudo user run:

sudo apt-get install postgresql-contrib-8.4

Switch to postgres user:

sudo su - postgres

Run:

psql -U DB_USER -d DB_NAME -f /usr/share/postgresql/8.4/contrib/pg_trgm.sql

Restart postgres

¢蛋碎的人ぎ生 2024-08-28 12:47:46

我在针对 Postgres 9.4 上使用 Django 1.11 ORM 进行三元相似性的函数运行 Django Test Runner 时遇到了同样的问题。

我必须做一些事情才能让它工作:

1) OP 是正确的,这需要启用 pg_trgm 扩展。然而,在 postgres9.4 中,这是基于每个数据库启用的。由于 Django 在每次运行时都会删除并重新创建测试数据库,因此新的测试数据库没有安装扩展。为了解决这个问题,我在 postgres 中默认新建的数据库模板中初始化了 pg_trgm 扩展。执行此操作的命令是以 postgres 用户身份运行 psql -d template1 -c 'CREATE EXTENSION pg_trgm;'

2) Postgres 必须重新启动

3) Django 测试运行程序无法识别这一点,所以我必须从 Django 1.11.12 升级到 1.11.18 (大概这在较新版本的 Django 中也已修复)

I was having this same issue in the context of running the Django Test Runner against a function that uses the Django 1.11 ORM for trigram similarity on Postgres 9.4.

I had to do a few things to get it working:

1) OP is correct that this required enabling the pg_trgm extension. However, in postgres9.4 this is enabled on a per-database basis. Since Django deletes and recreates the test database with each run, the new test database didn't have the extension installed. To fix this, I initialized the pg_trgm extension within the default newly-created database template in postgres. The command to do this is psql -d template1 -c 'CREATE EXTENSION pg_trgm;' run as the postgres user.

2) Postgres had to be restarted

3) The Django test runner wasn't recognizing this, so I had to upgrade from Django 1.11.12 to 1.11.18 (presumably this is also fixed in newer versions of Django)

御守 2024-08-28 12:47:46

以防其他人也陷入困境:假设数据库名为“ttrss”,我切换到 postgresql 的命令行:

sudo -u postgres psql --dbname=ttrss

然后在那里安装扩展:

CREATE EXTENSION pg_trgm;

in case others are struggling too: assuming that the db is called “ttrss” I switched to the command line of postgresql:

sudo -u postgres psql --dbname=ttrss

then install the extension there:

CREATE EXTENSION pg_trgm;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文