postgres中的文本搜索查询构建

时间:2010-06-04 05:15:01

标签: postgresql plpgsql

我需要构建一个存储过程,该过程接受输入变量数组。它将使用

等语法搜索这些内容
SELECT *
FROM mytable
WHERE search_index @@ to_tsquery(' ');

如果我给汤姆,迪克,哈里这样的程序提供输入 查询应该像

一样动态构建
SELECT *
FROM mytable
WHERE search_index @@ to_tsquery('tom | dick | harry ');


CREATE OR REPLACE FUNCTION text_search(search_terms character varying[], ent_id bigint)
  RETURNS entity_base AS

$BODY$DECLARE

  result_data entity_base;

  search_data text;
BEGIN

     search_data := array_to_string('search_terms[]', '|');

     SELECT * INTO result_data FROM entity_base WHERE search_index @@ to_tsquery(search_data) and entity_id = ent_id;
     RETURN result_data;
END;$BODY$
  LANGUAGE 'plpgsql' VOLATILE
  COST 100;

当我运行此错误时

ERROR:  could not determine polymorphic type because input has type "unknown"
CONTEXT:  SQL statement "SELECT  array_to_string('search_terms[]', '|')"
PL/pgSQL function "text_search" line 5 at assignment

********** Error **********

ERROR: could not determine polymorphic type because input has type "unknown"
SQL state: 42804
Context: SQL statement "SELECT  array_to_string('search_terms[]', '|')"
PL/pgSQL function "text_search" line 5 at assignment

1 个答案:

答案 0 :(得分:1)

使用array_to_string()。