Skip to content

Instantly share code, notes, and snippets.

@rorhug
Created April 1, 2014 16:51
Show Gist options
  • Select an option

  • Save rorhug/9918135 to your computer and use it in GitHub Desktop.

Select an option

Save rorhug/9918135 to your computer and use it in GitHub Desktop.
Postgres full text search example
# Note Load (30.9ms)
SELECT "notes".*, (
(ts_rank(
(
to_tsvector('english', unaccent(
coalesce("notes"."title"::text, '')
)) || to_tsvector('english', unaccent(
coalesce("notes"."short_text"::text, '')
))
),
(to_tsquery('english', ''' ' || unaccent('things') || ' ''')), 0)
)
)
AS pg_search_rank
FROM "notes"
WHERE "notes"."privacy" = 0
AND ((
(
to_tsvector('english', unaccent(
coalesce("notes"."title"::text, '')
)) || to_tsvector('english', unaccent(
coalesce("notes"."short_text"::text, '')
))
) @@
(to_tsquery('english', ''' ' || unaccent('things') || ' '''))
))
ORDER BY pg_search_rank DESC, "notes"."id" ASC, "notes"."vote_cache" DESC, "notes"."created_at" DESC
LIMIT 10 OFFSET 0;
SELECT COUNT(*)
FROM "notes"
WHERE "notes"."privacy" = 0
AND ((
(
to_tsvector('english', unaccent(
coalesce("notes"."title"::text, '')
)) || to_tsvector('english', unaccent(
coalesce("notes"."short_text"::text, ''))
)
) @@ (to_tsquery('english', ''' ' || unaccent('things') || ' '''))
));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment