Thread: tokenize string for tsearch?

tokenize string for tsearch?

From
Ottavio Campana
Date:
Hi, I'm trying to use tsearch2 for the first time and I'm having a
problem setting up a query

If I execute

SELECT * from test_table where ts_desc @@ to_tsquery ('hello&world');

it works, but I'm having the problem that the string used for the query
is not 'hello&world' but 'hello world', Moreover, it can have an
arbitrary number of spaces between the words, so I cannot just
substitute the spaces with &, because 'hello&&world' gives error.

What is the safest way transform a string into a list of works "anded"
together?

Thank you

--
Non c'e' piu' forza nella normalita', c'e' solo monotonia.


Attachment

Re: tokenize string for tsearch?

From
Oleg Bartunov
Date:
On Mon, 7 May 2007, Ottavio Campana wrote:

> Hi, I'm trying to use tsearch2 for the first time and I'm having a
> problem setting up a query
>
> If I execute
>
> SELECT * from test_table where ts_desc @@ to_tsquery ('hello&world');
>
> it works, but I'm having the problem that the string used for the query
> is not 'hello&world' but 'hello world', Moreover, it can have an
> arbitrary number of spaces between the words, so I cannot just
> substitute the spaces with &, because 'hello&&world' gives error.
>
> What is the safest way transform a string into a list of works "anded"
> together?

Don't worry, see how default parser works:

postgres=# select parse('default','hello     world');
     parse
--------------
  (1,hello)
  (12,"     ")
  (1,world)


btw, you can use plainto_tsquery for AND query

=# select plainto_tsquery('hello world');
   plainto_tsquery
-------------------
  'hello' & 'world'
(1 row)


     Regards,
         Oleg
_____________________________________________________________
Oleg Bartunov, Research Scientist, Head of AstroNet (www.astronet.ru),
Sternberg Astronomical Institute, Moscow University, Russia
Internet: oleg@sai.msu.su, http://www.sai.msu.su/~megera/
phone: +007(495)939-16-83, +007(495)939-23-83

Re: tokenize string for tsearch?

From
Magnus Hagander
Date:
On Mon, May 07, 2007 at 05:31:02PM -0700, Ottavio Campana wrote:
> Hi, I'm trying to use tsearch2 for the first time and I'm having a
> problem setting up a query
>
> If I execute
>
> SELECT * from test_table where ts_desc @@ to_tsquery ('hello&world');
>
> it works, but I'm having the problem that the string used for the query
> is not 'hello&world' but 'hello world', Moreover, it can have an
> arbitrary number of spaces between the words, so I cannot just
> substitute the spaces with &, because 'hello&&world' gives error.
>
> What is the safest way transform a string into a list of works "anded"
> together?

Look at plainto_tsquery().

//Magnus


Re: tokenize string for tsearch?

From
Ottavio Campana
Date:
Magnus Hagander wrote:
> On Mon, May 07, 2007 at 05:31:02PM -0700, Ottavio Campana wrote:
>> Hi, I'm trying to use tsearch2 for the first time and I'm having a
>> problem setting up a query
>>
>> If I execute
>>
>> SELECT * from test_table where ts_desc @@ to_tsquery ('hello&world');
>>
>> it works, but I'm having the problem that the string used for the query
>> is not 'hello&world' but 'hello world', Moreover, it can have an
>> arbitrary number of spaces between the words, so I cannot just
>> substitute the spaces with &, because 'hello&&world' gives error.
>>
>> What is the safest way transform a string into a list of words "anded"
>> together?
>
> Look at plainto_tsquery().

db=# SELECT plainto_tsquery('default', 'hello word');
ERROR:  function plainto_tsquery("unknown", "unknown") does not exist
HINT:  No function matches the given name and argument types. You may
need to add explicit type casts.

I'm using 8.1.8 and I don't find plainto_tsquery in tsearch2.sql

What can I do?

Thank you.

--
Non c'e' piu' forza nella normalita', c'e' solo monotonia.


Attachment

Re: tokenize string for tsearch?

From
Magnus Hagander
Date:
Ottavio Campana wrote:
> Magnus Hagander wrote:
>> On Mon, May 07, 2007 at 05:31:02PM -0700, Ottavio Campana wrote:
>>> Hi, I'm trying to use tsearch2 for the first time and I'm having a
>>> problem setting up a query
>>>
>>> If I execute
>>>
>>> SELECT * from test_table where ts_desc @@ to_tsquery ('hello&world');
>>>
>>> it works, but I'm having the problem that the string used for the query
>>> is not 'hello&world' but 'hello world', Moreover, it can have an
>>> arbitrary number of spaces between the words, so I cannot just
>>> substitute the spaces with &, because 'hello&&world' gives error.
>>>
>>> What is the safest way transform a string into a list of words "anded"
>>> together?
>> Look at plainto_tsquery().
>
> db=# SELECT plainto_tsquery('default', 'hello word');
> ERROR:  function plainto_tsquery("unknown", "unknown") does not exist
> HINT:  No function matches the given name and argument types. You may
> need to add explicit type casts.
>
> I'm using 8.1.8 and I don't find plainto_tsquery in tsearch2.sql
>
> What can I do?

Yeah, you need 8.2 for that function. I don't think anybody has tried
backpatching it, but if you want to you can look at the code in 8.2 and
see if you can backpatch it yourself. But the easiest way is certainly
to upgrade to 8.2.

//Magnus


Re: tokenize string for tsearch?

From
Ottavio Campana
Date:
Magnus Hagander wrote:
>> I'm using 8.1.8 and I don't find plainto_tsquery in tsearch2.sql
>>
>> What can I do?
>
> Yeah, you need 8.2 for that function. I don't think anybody has tried
> backpatching it, but if you want to you can look at the code in 8.2 and
> see if you can backpatch it yourself. But the easiest way is certainly
> to upgrade to 8.2.

doh! that's not possible. :-(

I'm solving with a custom stored procedure.

Thanks

--
Non c'e' piu' forza nella normalita', c'e' solo monotonia.


Attachment