Elephant_medium
Project / November 11, 2008

TSearchable - Postgres Text Search on Rails

By Dylan Stamat/3951 Views/2 Comments
## 50,000 articles, title and body indexed

## sphinx via ultrasphinx plugin
>> search = Ultrasphinx::Search.new(:query => "apple")
>> Benchmark.realtime { search.run }
=> 0.27561092376709
>> search = Ultrasphinx::Search.new(:query => "cat AND dog")
>> Benchmark.realtime { search.run }
=> 0.225758075714111
>> search = Ultrasphinx::Search.new(:query => "apple OR pear")
>> Benchmark.realtime { search.run }
=> 0.224870184998115

## postgres native text search via tsearchable
>> Benchmark.realtime { Article.find_by_text_search("apple") }
=> 0.156562089920044
>> Benchmark.realtime { Article.find_by_text_search("cat AND dog") }
=> 0.154358863830566
>> Benchmark.realtime { Article.find_by_text_search("apple OR pear") }
=> 0.164685010910034
2d7dd28875bf95321fdefd0517822511b20777ef_1226520150_0

Postgres Text Searching and Rails

As of Postgresql 8.3, text searching is now a native feature. Previous versions of Postgres required a "tsearch" extension, which was quite popular, and worked quite well.

As a fan of Postgres, I wanted to use its text search in a project I was working on. We had recently upgraded to 8.3, and knew the perks of tsearch now being a native feature. No socket trickery, ongoing support, etc. I found a great plugin called acts_as_tsearch which supported text search in <= 8.2, but didn't quite work with 8.3. It also had quite a few features that I didn't need. So, using acts_as_tsearch as a resource, I whipped up tsearchable, which met my needs. See the README for the list of available features.

The benchmarks shown above are obviously rough. They are against a production data set of approximately 50,000 records, with one varchar and one text field indexed. This benchmark was run on my local machine, with tons of other random processes running, and each search was run cold. Some thorough benchmarks on Sphinx and Postgres Text Search would be great however, as the 8.3 integration of tsearch was quite an impressive feat.

We are running the tsearchable in production across a few high profile sites, and it works great. The 8.3 install hums and search is super fast. Thanks to Florent Monbillard for his trigger, suggestions, and other contributions as well. There are a lot of features to be added, and a lot of love would do the plugin good... so, if you're running 8.3, feel free to use and contribute !

Comments

Posted by Andrei Erdoss on 5 months agoE7349346374a0c685bf8055b7977af5c?s=30

Good job on this plugin. Any plans to maintain it and turn it into a gem? I would like to use it with Heroku which at this point uses Postgresql 8.3.5 but it has a limited amount of options when it comes to full text searching (acts_as_ferret and solr - hosted on a another server).

Posted by Dylan Stamat on 5 months ago5e65c43f8c95e30cdf84b248718840d8?s=30

Hey Andrei! I haven't had the need to extend it into a gem just yet, but it would definitely be a nice improvement. Would you still be able to use it on Heroku as a plugin?

Add a Comment