Text search with tsvector and raw SQL
It is very common to implement a feature of searching through the contents of the database. In one of the previous articles, we learned how to implement it in…
It is very common to implement a feature of searching through the contents of the database. In one of the previous articles, we learned how to implement it in a simple way using pattern matching. Today we take it a step further and learn about the data types explicitly designed for full-text search. Text Search Types PostgreSQL provides two data types that help us implement full-text search. They allow us to search through a collection of texts and find the ones that match a given query the most. tsvector The tsvector column stores the text in a format optimized for search. To parse a string into the tsvector format, we need the to_tsvector function.SELECT to_tsvector('english', 'The quick brown fox quickly jumps over the lazy dog');When we look at the result of the above query, we notice a set of optimizations. One of the most apparent is grouping duplicates. Thanks to using the English dictionary, PostgreSQL noticed that “quick” and “quickly” are two variants of the same word. Also, using the tsvector type can help us filter out stop words. They are very common, appear in almost every sentence, and don’t have much value when searching through text. Since we used the English dictionary in the above example, PostgreSQL filtered out the words “the” and “over”. tsquery The tsquery data type stores the text we want to search for. To transform a string into the tsquery format, we can use the to_tsquery function.SELECT to_tsquery('fox'); To check if a certain tsvector matches the tsquery, we need to use the @@ operator.SELECT to_tsvector('english', 'The quick brown fox quickly jumps over the lazy dog') @@ to_tsquery('fox'); true When doing the above, we can play with the &, |, and ! boolean operators. For example, we can use the ! operator to make sure a given text does not contain a particular word.SELECT to_tsvector('english', 'The quick brown fox quickly jumps over the lazy dog') @@ to_tsquery('!cat'); true Check out the official documentation for a good explanation of all available operators. Another handy function is plainto_tsquery. It takes an unformatted phrase and inserts the & operator between words. Because of that, it is an excellent choice to handle the input from the user.SELECT to_tsvector('english', 'The quick brown fox quickly jumps over the lazy dog') @@ plainto_tsquery('brown fox'); true Transforming the existing data Let’s take a look at our posts table.CREATE TABLE posts ( id int GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, title text NOT NULL, post_content text NOT NULL, author_id int REFERENCES users(id) NOT NULL )Unfortunately, it does not contain a tsvector column. The most straightforward solution to the above problem is to convert our data to tsvector on the fly.SELECT * FROM posts WHERE to_tsvector('english', post_content) @@ plainto_tsquery('fox');We can take the above even further and combine the contents of the title and post_content columns to search through both.SELECT * FROM posts WHERE to_tsvector('english', post_content || ' ' || title) @@ plainto_tsquery('fox');The crucial issue with the above approach is that it causes PostgreSQL to transform the text from every record of the posts database, which can take a substantial amount of time. Instead, I suggest defining a generated column that contains the data transformed into the tsvector format.ALTER TABLE posts ADD COLUMN text_tsvector tsvector GENERATED ALWAYS AS ( to_tsvector('english', post_content || ' ' || title) ) STORED If you want to know moure about generated columns, check out Defining generated columns with PostgreSQL and TypeORM Since we use the STORED keyword, we define a stored generated column that is saved in our database. PostgreSQL updates it automatically every time we modify the post_content and title columns. We can now use our generated column when making a SELECT query to improve its performance drastically.SELECT * FROM posts WHERE text_tsvector @@ plainto_tsquery('fox'); Ordering the results So far, we haven’t paid attention to the order of the results of our SELECT query. Sorting the search results based on relevance could help the users quite a bit. For example, we can indicate that the text from the title column is more important than the post_content column. To do that, let’s change how we create our text_tsvector column and use the setweight function.ALTER TABLE posts ADD COLUMN text_tsvector tsvector GENERATED ALWAYS AS ( setweight(to_tsvector('english', title), 'A') || setweight(to_tsvector('english', post_content), 'B') ) STOREDLet’s compare the two following posts after modifying the text_tsvector column: The combined value of the title and post_content is the same in both posts. However, the text_tsvector takes into account that the title column is more important. Thanks to the above, we can now use the ts_rank function to order our results based on the weight of each column.SELECT * FROM posts WHERE text_tsvector @@ plainto_tsquery('brown fox') ORDER BY ts_rank(text_tsvector, plainto_tsquery('brown fox')) DESC Implementing full-text search with NestJS Let’s create a migration first to implement the above functionalities in our NestJS project.npx knex migrate:make add_post_tsvector 20221113211441_add_post_tsvector.ts import { Knex } from 'knex'; export async function up(knex: Knex): Promise