tsearch2: setting weights on tsquery plainto_tsquery with | or and weight - Mailing list pgsql-general

From Ivan Sergio Borgonovo
Subject tsearch2: setting weights on tsquery plainto_tsquery with | or and weight
Date
Msg-id 20081021203726.15c87109@dawn.webthatworks.it
Whole thread Raw
In response to Re: exposing more parse was: Re: tsearch2: setting weights on tsquery  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
On Tue, 21 Oct 2008 13:40:33 -0400
Tom Lane <tgl@sss.pgh.pa.us> wrote:

> Ivan Sergio Borgonovo <mail@webthatworks.it> writes:
> > I missed it. Thanks. Nearly perfect. Now I've to understand what
> > a {} is.
> > An array with a null element? an empty array? an array
> > containing ''?
>
> Hmm ... it appears that ts_lexize is returning a one-dimensional
> array of no elements, whereas '{}'::text[] returns a
> zero-dimensional array; as does array[]::text[] so you can't
> easily get around it :-(

Maybe in 8.4. I'm already thankful this toy is making such wonders.

> We probably ought to clean this up...
>
> In the meantime it looks like you could check for
> "array_upper(lexemes,1) > 0".

My version as posted before was the >=1 variant.

This is what I was trying to obtain a weighted version of
plainto_tsquery with parametrised operator:

I think I'm wasting some cpu cycles when ts_debug try to fill the
lexemes array that I'm not using.
So maybe there should be more finer access to the functions to help
build up tsquery.
The ts_parse is missing the information about what tokens should be
discarded.

create or replace function setweight(configuration regconfig,
queryin text, weight char, op char ,out query tsquery) as
$$
declare
    tsdebug record;
    querystring text;
begin
querystring:='';
    for tsdebug in select token from
        ts_debug(configuration, queryin)
            where array_upper(lexemes,1)>0 loop
        querystring:=querystring ||
        coalesce(tsdebug.token ||
        coalesce(':' || weight, '') ||
        ' ', '');
    end loop;
    querystring:=regexp_replace(querystring, ' (.)',
        op || '\\1', 'g');
    query:=to_tsquery(configuration, querystring);
    return;
end;
$$ language plpgsql immutable;


--
Ivan Sergio Borgonovo
http://www.webthatworks.it


pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: exposing more parse was: Re: tsearch2: setting weights on tsquery
Next
From: "Glen Brown"
Date:
Subject: Slow to delete TOAST table data