tsearch2 add resultset to index
How can I add开发者_Go百科 a resultset (more than one entry) to a tsvector? I use postgres 8.3. I have an m-n relationship and I'd like to have all values from one column of the n-side in the tsvector of the m-side. This statement will work if I have an limit to one the subselect. But not without the limit.
UPDATE mytable
SET mytsvector=to_tsvector('english',
coalesce(column_a, '') ||' '||
coalesce((SELECT item FROM other_table WHERE id = other_id LIMIT 1), '')
)
ERROR: more than one row returned by a subquery used as an expression
Under Postgres 8.3 at first I have to create an aggregate function to generate an array from an select.
CREATE AGGREGATE array_accum (
sfunc = array_append,
basetype = anyelement,
stype = anyarray,
initcond = '{}'
);
Since 8.4 there is the function array_agg().
The hole statement looks like this:
UPDATE mytable
SET mytsvector=to_tsvector('english',
coalesce(column_a, '') ||' '||
coalesce(
(SELECT array_to_string(array_accum(item), ' ')
FROM mytable m, other_table o
WHERE o.id = m.other_id AND m.id = id GROUP BY m.id), '')
)
精彩评论