API with NestJS #179. Pattern matching search with Drizzle ORM and PostgreSQL

NestJS

This entry is part 179 of 183 in the API with NestJS

Searching through text documents is a very common feature in many web applications. In this article, we learn how to implement it using pattern matching using the Drizzle ORM, PostgreSQL, and NestJS.

Pattern matching with LIKE and ILIKE

With pattern matching, we can determine if a given piece of text matches a particular pattern. To do that, we need to use the operator built into SQL.

In the above example, the operator works like an equals operator and returns true only if the text exactly matches the pattern. To change that, we can use the operator that matches a sequence of zero or more characters.

We can also use the operator to match a single character.

If we want to search for a piece of text in an entire string, we should wrap it with the sign on both the left and right sides.

Making the search case-insensitive

If we want to make our search case-insensitive, we can use the operator.

Filtering rows

We can use the above operators to filter the rows of a table.

Implementing searching with NestJS

To implement the above functionality with NestJS, we need to allow the user to provide the pattern through query parameters. To do that, we should create a DTO class to validate the parameter.

search-articles-query.dto.ts

Now, we can use our DTO in the controller.

articles.controller.ts

Pattern matching with Drizzle ORM

To use the operator with the Drizzle ORM to filter rows, we need to combine the and functions.

articles.service.ts

Please notice that we’re not expecting the users to provide the operators explicitly. Instead, we’re wrapping their input with . This might work in unexpected ways if the user puts the or characters in their input. To prevent this, we could sanitize the provided string by prepending all special characters with the sign.

If we want our search to be case-insensitive, we can use the function.

articles.service.ts

Searching through multiple columns

Right now, we’re searching only through one column. We can use the function if we want to use multiple columns.

articles.service.ts

Pattern matching vs. Full-text search

In one of the previous articles, we covered how to use the Drizzle ORM to implement Full-text search using the column. While this method is more complicated to implement, it is also more powerful.

Let’s take a look at the example with the following sentences:

  1. Birds fly south for the winter.
  2. We saw a helicopter flying above the city.
  3. We flew to San Francisco last summer.
  4. The years have flown by quickly.

With pattern matching, we’re looking for an exact match. If we use the LIKE operator to look for the word “fly”, we will only match sentences one and two because they contain the word “fly”.

However, with the full-text search feature, each word goes through the stemming process, where it is simplified to its root form. Because of that, the words “flying“, “flew“, and “flown” are all simplified to the word “fly”. When the user tries to search for sentences that include the word “fly”, all the above sentences match. This improves the search process a lot and returns more meaningful results.

Regular expressions

While we could cover a bunch of real-life scenarios with the operator, it might not be enough in some cases. Fortunately, PostgreSQL allows us to use regular expressions.

The SQL standard also includes the operator that’s a blend of of the operator and regular expressions.

While Drizzle ORM does not natively support it, we could write a piece of raw SQL to use it.

Summary

In this article, we explained pattern matching and implemented it using raw SQL queries and the Drizzle ORM. We also learned how to support the search feature in our REST API with NestJS and explained how pattern matching differs from full-text search. This helped us understand when pattern matching is good enough and why we might want to choose full-text search instead.

Series Navigation<< API with NestJS #178. Storing files inside of a PostgreSQL database with DrizzleAPI with NestJS #180. Organizing Drizzle ORM schema with PostgreSQL >>
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments