tsearch2 trigger alternative

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Chris Gamache

    tsearch2 trigger alternative

    Tsearch2 comes with its own tsearch2 trigger function. You pass column names to
    it, and it puts a vanilla tsvector into the column named in TG_ARGV[0]. Not
    only can you pass column names to it, but you can pass simple functions to it
    as well. This is magical to me. :)

    I'm trying to figure out how to do the same thing using plpgsql, except instead
    of returning a vanilla tsvector, I want to return a specially weighted
    tsvector. I've created a function that can do this:

    create or replace function name_vector (text) returns tsvector as 'select
    setweight(to_ts vector(substr($ 1,1,strpos($1,' ',''))),''C'') ||
    to_tsvector(sub str($1,strpos($ 1,'','')+1,leng th($1)));' language 'sql';

    so...

    Plain:

    select to_tsvector('Ei nstein, Albert');
    to_tsvector
    -------------------------
    'albert':2 'einstein':1

    Weighted:

    select name_vector('Ei nstein, Albert');
    name_vector
    --------------------------
    'albert':2 'einstein':1C


    Now, to somehow package that into a magical trigger function...

    All the examples for creating trigger functions that I've found use static
    column names, NEW and OLD ... I would like to create a generic trigger
    function, as the tsearch2 trigger function does, to return the specially
    weighted tsvector.

    Its like a lighter to a caveman. Can anyone lend a hand?

    CG

    _______________ _______________ ____
    Do you Yahoo!?
    Yahoo! Mail SpamGuard - Read only the mail you want.


    ---------------------------(end of broadcast)---------------------------
    TIP 4: Don't 'kill -9' the postmaster

  • Teodor Sigaev

    #2
    Re: tsearch2 trigger alternative



    Chris Gamache wrote:[color=blue]
    > Tsearch2 comes with its own tsearch2 trigger function. You pass column names to
    > it, and it puts a vanilla tsvector into the column named in TG_ARGV[0]. Not
    > only can you pass column names to it, but you can pass simple functions to it
    > as well. This is magical to me. :)[/color]
    look at tsvector.c:864

    numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
    if (numattr == SPI_ERROR_NOATT RIBUTE)
    {
    funcoid = findFunc(trigge r->tgargs[i]);
    if (funcoid == InvalidOid)
    ereport(ERROR,
    (errcode(ERRCOD E_UNDEFINED_COL UMN),
    errmsg("could not find function or field \"%s\"",
    trigger->tgargs[i])));
    continue;
    }

    If current args (trigger->tgargs[i]) isn't a column name, then it's a function
    name. It's simple :)

    [color=blue]
    >
    > I'm trying to figure out how to do the same thing using plpgsql, except instead
    > of returning a vanilla tsvector, I want to return a specially weighted
    > tsvector. I've created a function that can do this:[/color]
    I didn't work with plpgsql :(

    --
    Teodor Sigaev E-mail: teodor@sigaev.r u

    ---------------------------(end of broadcast)---------------------------
    TIP 5: Have you checked our extensive FAQ?



    Comment

    • Chris Gamache

      #3
      Re: tsearch2 trigger alternative


      --- Teodor Sigaev <teodor@sigaev. ru> wrote:[color=blue]
      >
      >
      > Chris Gamache wrote:[color=green]
      > > Tsearch2 comes with its own tsearch2 trigger function. You pass column[/color]
      > names to[color=green]
      > > it, and it puts a vanilla tsvector into the column named in TG_ARGV[0]. Not
      > > only can you pass column names to it, but you can pass simple functions to[/color]
      > it[color=green]
      > > as well. This is magical to me. :)[/color]
      > look at tsvector.c:864
      >
      > numattr = SPI_fnumber(rel->rd_att, trigger->tgargs[i]);
      > if (numattr == SPI_ERROR_NOATT RIBUTE)
      > {
      > funcoid = findFunc(trigge r->tgargs[i]);
      > if (funcoid == InvalidOid)
      > ereport(ERROR,
      > (errcode(ERRCOD E_UNDEFINED_COL UMN),
      > errmsg("could not find function or field \"%s\"",
      > trigger->tgargs[i])));
      > continue;
      > }
      >
      > If current args (trigger->tgargs[i]) isn't a column name, then it's a
      > function
      > name. It's simple :)[/color]

      Its like a circuit board to Genghis Kahn... :) I still wouldn't be able to
      quickly write my own trigger. It occurrs to me that this might be an
      opportunity to properly extend tsearch2, though.

      I'm sure that the tsearch2 trigger could detect a tsvector type and allow it to
      be inserted without modification. That would solve this problem completely! I
      can't think of any practical argument against it. It makes sense that others
      besides myself would want to selectively weight columns upon insert without
      having to write their own custom trigger in C.

      CG

      _______________ _______________ ____
      Do you Yahoo!?
      Yahoo! Mail SpamGuard - Read only the mail you want.


      ---------------------------(end of broadcast)---------------------------
      TIP 7: don't forget to increase your free space map settings

      Comment

      Working...