-
+
Full Text Search
SELECT title
FROM pgweb
-WHERE to_tsvector(title || body) @@ to_tsquery('create & table')
+WHERE to_tsvector(title || ' ' || body) @@ to_tsquery('create & table')
ORDER BY last_mod_date DESC LIMIT 10;
- For clarity we omitted the coalesce function
- which would be needed to search rows that contain NULL
+ For clarity we omitted the coalesce function calls
+ which would be needed to find rows that contain NULL
in one of the two fields.
Indexes can even concatenate columns:
-CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector('english', title || body));
+CREATE INDEX pgweb_idx ON pgweb USING gin(to_tsvector('english', title || ' ' || body));
ALTER TABLE pgweb ADD COLUMN textsearchable_index_col tsvector;
UPDATE pgweb SET textsearchable_index_col =
- to_tsvector('english', coalesce(title,'') || coalesce(body,''));
+ to_tsvector('english', coalesce(title,'') || ' ' || coalesce(body,''));
Then we create a
GIN index to speed up the search: