Postgresql 全文搜索不产生任何结果
我有以下看法:Postgresql 全文搜索不产生任何结果,postgresql,full-text-search,sql-view,postgres-9.6,Postgresql,Full Text Search,Sql View,Postgres 9.6,我有以下看法: CREATE VIEW public.profiles_search AS SELECT profiles.id, profiles.bio, profiles.title, ( setweight(to_tsvector(profiles.search_language::regconfig, profiles.title::text), 'B'::"char") ||
CREATE VIEW public.profiles_search AS
SELECT
profiles.id,
profiles.bio,
profiles.title,
(
setweight(to_tsvector(profiles.search_language::regconfig, profiles.title::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.bio), 'A'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.category::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, array_to_string(profiles.tags, ',', '*')), 'C'::"char")
) AS document
FROM profiles
GROUP BY profiles.id;
但是,如果profiles.tags为空,则文档
为空,即使其余字段(title、bio和category)包含数据
是否有某种方法使make that字段成为可选字段,这样它具有空数据不会导致空文档?这似乎是常见的字符串连接问题-连接
NULL
值会使整个结果NULL
建议您始终使用coalesce()
为任何输入提供默认值:
如果您不想为复杂数据类型提供默认值(如@approxiblue建议的coalesce(profiles.tags,ARRAY[]::text[])
),我想您可以简单地执行以下操作:
CREATE VIEW public.profiles_search AS
SELECT
profiles.id,
profiles.bio,
profiles.title,
(
setweight(to_tsvector(profiles.search_language::regconfig, profiles.title::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.bio), 'A'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.category::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, coalesce(array_to_string(profiles.tags, ',', '*'), '')), 'C'::"char")
) AS document
FROM profiles
GROUP BY profiles.id;
在这些情况下profiles.tags是否为空?如果使用
coalesce(profiles.tags,ARRAY[]::text[])
而不是profiles.tags(假设它是一个文本数组),默认为空数组而不是空数组,它会工作吗?
CREATE VIEW public.profiles_search AS
SELECT
profiles.id,
profiles.bio,
profiles.title,
(
setweight(to_tsvector(profiles.search_language::regconfig, profiles.title::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.bio), 'A'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, profiles.category::text), 'B'::"char") ||
setweight(to_tsvector(profiles.search_language::regconfig, coalesce(array_to_string(profiles.tags, ',', '*'), '')), 'C'::"char")
) AS document
FROM profiles
GROUP BY profiles.id;