Following on from this answer I want to know what the best way to use PostgreSQL\'s built-in full text search is if I want to sort by rank, and limit to only matching q
If I use the version with repeated to_tsvector(...) will it call that twice, or is it smart enough to cache the results somehow?
The best way to notice these things is to do a simple explain, although those can be hard to read.
Long story short, yes, PostgreSQL is smart enough to reuse computed results.
Is there a way to do it without repeating the to_ts... function calls?
What I usually do is add a tsv
column which is the text search vector. If you make this auto update using triggers it immediately gives you the vector easily accessible but it also allows you to selectively update the search index by making the trigger selective.
Is there a way to use score in the WHERE clause at all?
Yes, but not with that name. Alternatively you could create a sub-query, but I would personally just repeat it.
If there is, would it be better to filter by score > 0 or use the @@ thing?
The simplest version I can think of is this:
SELECT *
FROM pictures
WHERE 'small dog' @@ text_search_vector
The text_search_vector
could obviously be replaced with something like to_tsvector('english', pictures.title)