How to Add PostgreSQL Full-text Search to Ruby Apps and Optimize Its Performance

Pavel Astraukh

In this post, I describe how to implement a built-in PostgreSQL full-text search and add it to your to your Ruby on Rails application. I demonstrate how it works on a test application that searches articles by title, content, author’s name, and comments. In addition, I explain how to speed up search by eliminating excessive join queries that greatly slow down system’s performance. Visit my GitHub page to read the article and view code samples.

include PgSearch

pg_search_scope :search,
                against: [:title, :content],
                associated_against: {
                  author: :name,
                  comments: :content
                using: {
                  tsearch: {
                    dictionary: 'english',
                    any_word: true,
                    prefix: true
No Comments

Benchmarks and Research

Subscribe to new posts

Get new posts right in your inbox!