Full-Text 搜索无结果

Full-Text Search producing no results

我有以下观点:

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 为空,则 document 为空,即使其余字段(标题、个人简介和类别)包含数据。

有没有什么方法可以使该字段成为可选字段,使其具有空数据不会导致空文档?

这似乎是常见的字符串连接问题 - 连接一个 NULL 值会使整个结果成为 NULL.

Here 建议您始终为具有 coalesce():

的任何输入提供默认值
UPDATE tt SET ti =
    setweight(to_tsvector(coalesce(title,'')), 'A')    ||
    setweight(to_tsvector(coalesce(keyword,'')), 'B')  ||
    setweight(to_tsvector(coalesce(abstract,'')), 'C') ||
    setweight(to_tsvector(coalesce(body,'')), 'D');

如果您不想为复杂数据类型提供默认值(如@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;