首页 > 解决方案 > postgresql [42883] 错误:函数 to_tsvector("unknown", "unknown") 不存在

问题描述

我是 postgresql 的新手,正在尝试使用全文搜索,to_tsvector但是我遇到了错误。

SQL 和错误

SELECT to_tsvector('english', 'The quick brown fox jumped over the lazy dog.');

[42883] ERROR: function to_tsvector("unknown", "unknown") does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts.

不同尝试的 SQL 和错误

SELECT to_tsvector('english'::character, 'The quick brown fox jumped over the lazy dog.'::character);

[42883] ERROR: function to_tsvector(character, character) does not exist Hint: No function matches the given name and argument types. You may need to add explicit type casts.

这令人沮丧,因为这感觉就像让 to_tsvector 工作的“你好世界”,但是我什至无法让它返回。我将 DataGrip 2020.2 与 Postgres 一起使用,但不确定如何查看我使用的 postgres 版本(我认为它是较新版本)。我上面的代码有明显的错误吗?

标签: postgresqlfull-text-search

解决方案


您可以尝试检查使用了哪些类型(我使用的是psql客户端):

postgres=# \df to_tsvector
                             List of functions
┌────────────┬─────────────┬──────────────────┬─────────────────────┬──────┐
│   Schema   │    Name     │ Result data type │ Argument data types │ Type │
╞════════════╪═════════════╪══════════════════╪═════════════════════╪══════╡
│ pg_catalog │ to_tsvector │ tsvector         │ json                │ func │
│ pg_catalog │ to_tsvector │ tsvector         │ jsonb               │ func │
│ pg_catalog │ to_tsvector │ tsvector         │ regconfig, json     │ func │
│ pg_catalog │ to_tsvector │ tsvector         │ regconfig, jsonb    │ func │
│ pg_catalog │ to_tsvector │ tsvector         │ regconfig, text     │ func │
│ pg_catalog │ to_tsvector │ tsvector         │ text                │ func │
└────────────┴─────────────┴──────────────────┴─────────────────────┴──────┘
(6 rows)

charactertype ,没有任何变体character

您的第一个查询正在我的 comp 中工作。请检查您使用的 Postgres 版本。较旧的(非常旧的 - 不支持的版本)Postgres 没有此功能

postgres=# SELECT to_tsvector('english', 'The quick brown fox jumped over the    lazy dog.');
┌───────────────────────────────────────────────────────┐
│                      to_tsvector                      │
╞═══════════════════════════════════════════════════════╡
│ 'brown':3 'dog':9 'fox':4 'jump':5 'lazi':8 'quick':2 │
└───────────────────────────────────────────────────────┘
(1 row)

当你想使用显式类型时,你可以使用regconfigand text

postgres=# SELECT to_tsvector('english'::regconfig, 
                   'The quick brown fox jumped over the lazy dog.'::text);
┌───────────────────────────────────────────────────────┐
│                      to_tsvector                      │
╞═══════════════════════════════════════════════════════╡
│ 'brown':3 'dog':9 'fox':4 'jump':5 'lazi':8 'quick':2 │
└───────────────────────────────────────────────────────┘
(1 row)

推荐阅读