Re: pg_trgm module: no convertion into Trigrams on one side when comparing

Поиск
Список
Период
Сортировка
От Amit Langote
Тема Re: pg_trgm module: no convertion into Trigrams on one side when comparing
Дата
Msg-id CA+HiwqH2kxZ_K4y5Noj0w2=4HPoKNVE4mOryfWXcmoHr9QnGUA@mail.gmail.com
обсуждение исходный текст
Ответ на pg_trgm module: no convertion into Trigrams on one side when comparing  ("Janek Sendrowski" <janek12@web.de>)
Ответы Re: pg_trgm module: no convertion into Trigrams on one side when comparing
Список pgsql-general
On Thu, Nov 14, 2013 at 2:37 AM, Janek Sendrowski <janek12@web.de> wrote:
> Hi,
> I'm using the pg_trgm module,
> Is there a possibility not to convert one side of the string when comparing?
> I need a kind of reference string containing only certain trigrams.
> It's not possible to build every kind of string using the function.
>
> example:
> Code: Alles auswählen
> SELECT show_trgm('abc');
> show_trgm
> -------------------------
> {" a"," ab",abc,"bc "}
> (1 row)
>
> But I like to have only {'abc'} for example.
>
> I would use this function: "similarity(text, text)"
> Could it somehow work with explicit data-type definitions or maybe with putting the string in brackets or quoting?
> Or do I have to change the source code?
>

I guess you're looking for simple pattern matching; something like:

column LIKE '%abc%'

postgres=# create table foo(a text);
CREATE TABLE
postgres=# insert into foo values ('ab'), ('abc'), ('gabcd'), ('xabf');
INSERT 0 4

-- similarity() threshold
postgres=# select show_limit();
 show_limit
------------
        0.3
(1 row)

-- '%' is similarity operator which returns true if column value is
"sufficiently similar" to key (in this case 'abc'). This is determined
by the number of tri-grams two strings share.
postgres=# select *,similarity(a, 'abc') from foo where a % 'abc';
  a  | similarity
-----+------------
 ab  |        0.4
 abc |          1
(2 rows)

-- And finally, probably what you're looking for. Simple pattern matching.
postgres=# select * from foo where a LIKE '%abc%';
   a
-------
 abc
 gabcd
(2 rows)

-- You could go ahead and add more trigrams that you'd want result to contain.
postgres=# insert into foo values ('gabcddfg');
INSERT 0 4

postgres=# select * from foo where a LIKE '%abc%dfg%';
    a
----------
 gabcddfg
(1 row)

Is this what you want?

--
Amit


В списке pgsql-general по дате отправления:

Предыдущее
От: Tatsuo Ishii
Дата:
Сообщение: Re: what checksum algo?
Следующее
От: walerina
Дата:
Сообщение: Re: How to print out a mass of text messages from a Samsung smart phone easily?