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

by Pavel AstraukhSeptember 11, 2013
Learn how to enable a full-text search by a title, content, author's name, and comments for a RoR application.

In this post, we describe how to implement a built-in PostgreSQL full-text search and add it to your Ruby on Rails application. We demonstrate how it works on a test application that searches articles by a title, content, the author’s name, and comments.

In addition, we explain how to speed up search by eliminating excessive join queries that greatly slow down the system’s performance. Visit this 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
                  }
                }

 

Further reading


This post was written by Pavel Astraukh and edited by Alex Khizhniak.