Received: from maia.hub.org (maia-3.hub.org [200.46.204.243]) by mail.postgresql.org (Postfix) with ESMTP id 32956632287 for ; Sun, 8 Aug 2010 12:51:21 -0300 (ADT) Received: from mail.postgresql.org ([200.46.204.86]) by maia.hub.org (mx1.hub.org [200.46.204.243]) (amavisd-maia, port 10024) with ESMTP id 87951-05 for ; Sun, 8 Aug 2010 15:51:14 +0000 (UTC) X-Greylist: from auto-whitelisted by SQLgrey-1.7.6 Received: from sss.pgh.pa.us (sss.pgh.pa.us [66.207.139.130]) by mail.postgresql.org (Postfix) with ESMTP id 31690632CFA for ; Sun, 8 Aug 2010 12:51:14 -0300 (ADT) Received: from sss2.sss.pgh.pa.us (tgl@localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.2/8.14.2) with ESMTP id o78FpCfS003729; Sun, 8 Aug 2010 11:51:12 -0400 (EDT) To: =?ISO-8859-1?Q?Anders_=D8stergaard_Jensen?= cc: pgsql-sql@postgresql.org Subject: Re: tsearch2 query question In-reply-to: References: Comments: In-reply-to =?ISO-8859-1?Q?Anders_=D8stergaard_Jensen?= message dated "Sun, 08 Aug 2010 12:59:59 +1000" Date: Sun, 08 Aug 2010 11:51:12 -0400 Message-ID: <3728.1281282672@sss.pgh.pa.us> From: Tom Lane X-Virus-Scanned: Maia Mailguard 1.0.1 X-Spam-Status: No, hits=-1.91 tagged_above=-5 required=5 tests=BAYES_00=-1.9, T_RP_MATCHES_RCVD=-0.01 X-Spam-Level: X-Archive-Number: 201008/41 X-Sequence-Number: 34481 =?ISO-8859-1?Q?Anders_=D8stergaard_Jensen?= writes: > Now, let's start out with a classic, non-tsearch2 ILIKE query with two > wildcards around the search token: > metabase=# select id, name from customers where name ilike '%holstebr%'; > id | name > ------+------------------- > 3646 | Holstebro Kommune > (1 row) > I am trying to do the same exactly with tsearch2 but with no luck: > metabase=# select id, name from customers where search_idx @@ > to_tsquery('*Holstebr*'); There isn't any exact equivalent for that in tsquery, because it's about searching for words not arbitrary substrings. You could do to_tsquery('Holstebro'). Since 8.4 you could also do a prefix match, say to_tsquery('Holstebr:*'), but there's no way to omit letters from the start of the word. Also, you have to be wary of prefix matches if you use a stemming dictionary, because dropping characters from the end of the word might make it stem differently. > And subsequently: how do I handle spaces in between, fx. so that > 'holstebro komm*' yields 'holstebro kommune'? You need to think of that as being two independent word searches, like to_tsquery('holstebro & komm:*'). AFAIR there isn't any phrase matching per se in tsquery. Of course, you could AND this with an ILIKE clause to filter out matches where the words weren't adjacent. If you're really dependent on the exact behavior of LIKE-style searching then tsquery is not going to provide you with an exact replacement. You might consider looking at contrib/pg_trgm/ to find an indexable operation that can speed up LIKE searches. regards, tom lane