【问题标题】:Full-Text Search producing no results全文搜索不产生结果
【发布时间】:2018-07-17 02:51:59
【问题描述】:

我有以下观点:

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

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

【问题讨论】:

  • profiles.tags 在这些情况下是否为空?如果你使用coalesce(profiles.tags, ARRAY[]::text[])而不是profiles.tags(假设它是一个文本数组),默认为一个空数组而不是null,它会工作吗?

标签: postgresql full-text-search sql-view postgres-9.6


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-07-17
    • 1970-01-01
    • 1970-01-01
    • 2022-01-09
    • 2013-09-08
    • 2020-05-06
    • 2013-12-06
    相关资源
    最近更新 更多