Re: joins with text search

From: Sam Mason <sam(at)samason(dot)me(dot)uk>
To: pgsql-general(at)postgresql(dot)org
Subject: Re: joins with text search
Date: 2010-09-08 09:52:03
Message-ID: 20100908095203.GV2243@samason.me.uk
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Tue, Sep 07, 2010 at 10:42:53PM -0400, pg(at)slatech(dot)com wrote:
> i am currently populating the textsearch column with the following
> command:
>
> UPDATE
> products
> SET
> textsearch=setweight(to_tsvector('english', description), 'A') ||
> setweight(to_tsvector('english', part_number, 'B')
> WHERE
> product_list_id=3
>
>
> Is there a way I can join products.vendor_id with vendors.id and get the
> vendor.name in the textsearch column as well?

Yup, you can either do a join inside the UPDATE, or do a subquery. Here
it's probably easiest to do a correlated subquery:

UPDATE products p SET textsearch=nullif('',
coalesce(setweight(to_tsvector('english', p.description), 'A'),'') ||
coalesce(setweight(to_tsvector('english', p.part_number), 'B'),'') ||
coalesce(setweight(to_tsvector('english', (
SELECT v.name FROM vendors v WHERE v.id = p.vendor_id)), 'A'),''))
WHERE p.product_list_id=3;

I've put the calls to coalesce in so that if you happen to have any
fields missing you'll still get a useful text search column.

I'd also be tempted to remove the "to_tsvector" call from the
part_number, as I doubt it really is an english bit of text. You can
probably just use it as a tsvector literal, probably quoting it first,
maybe something like:

coalesce(setweight(quote_literal(p.part_number)::tsvector,'B'),'')

--
Sam http://samason.me.uk/

In response to

Browse pgsql-general by date

  From Date Subject
Next Message jackassplus 2010-09-08 15:03:47 how do i count() similar items
Previous Message Dave Page 2010-09-08 08:56:24 Re: Postgres 32bit on Windows 64bit, related components