postgresql [42883] ERROR: function to_tsvector("unknown", "unknown") does not exist

postgresql [42883] ERROR: function to_tsvector("unknown", "unknown") does not exist

我是 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.

这令人沮丧,因为这感觉就像 'hello world' 让 to_tsvector 正常工作,但我什至无法做到 return。我在 Postgres 中使用 DataGrip 2020.2,但不确定如何查看我使用的是哪个版本的 postgres(我认为它是较新的版本)。我上面的代码有明显错误吗?

你可以试试看,使用的是什么类型(我用的是psql client`):

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)

charactercharacter 类型没有任何变体。

你的第一个查询在我的表格中有效。请检查您使用的 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)

当你想使用显式类型时,你可以使用regconfigtext:

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)