Last active
October 6, 2017 06:16
-
-
Save tirkarthi/3431fde569fd9ce7e4138fbd36623678 to your computer and use it in GitHub Desktop.
Postgres text search
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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