Skip to content

Instantly share code, notes, and snippets.

@tirkarthi
Last active October 6, 2017 06:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tirkarthi/3431fde569fd9ce7e4138fbd36623678 to your computer and use it in GitHub Desktop.
Save tirkarthi/3431fde569fd9ce7e4138fbd36623678 to your computer and use it in GitHub Desktop.
Postgres text search
example=# CREATE DATABASE test;
CREATE DATABASE
example=# \c test
test=# create table example(name varchar(100), description varchar(100));
CREATE TABLE
test=# insert into example values('test', 'test');
INSERT 0 1
test=# insert into example(name) values('test 12');
INSERT 0 1
test=# insert into example(name) values('test 13');
INSERT 0 1
test=# insert into example(name) values('test sample 14');
INSERT 0 1
test=# SELECT "example"."name", "example"."description", ts_rank(to_tsvector(COALESCE("example"."name") || ' ' || COALESCE("example"."description")), to_tsquery('test & sample')) AS "rank" FROM "example" ORDER BY "rank" DESC;
name | description | rank
----------------+-------------+-------
test 12 | |
test 13 | |
test sample 14 | |
test | test | 1e-20
(4 rows)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment