代码之家  ›  专栏  ›  技术社区  ›  Darrell Brogdon

全文搜索没有结果

  •  3
  • Darrell Brogdon  · 技术社区  · 7 年前

    我有以下观点:

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

    是否有某种方法使make that字段成为可选字段,使其具有空数据不会导致空文档?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Boris Schegolev    7 年前

    这似乎是常见的字符串连接问题-连接 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');
    

    如果不想为复杂数据类型提供默认值(如 coalesce(profiles.tags, ARRAY[]::text[]) 正如@approxiblue所建议的,我怀疑您可以简单地做到:

    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;