Skip to content
Logan Serman edited this page Feb 11, 2015 · 3 revisions

Records returned in an API can be searched using Postgres and the textacular gem. To enable search, install Textacular and Postgres, then require metova/search:

gem 'pg'
gem 'textacular'
gem 'metova', require: ['metova/search']

Basic Searching

The simplest form of search is an exact search. The attributes must include the exact query within it. Without any specific attribute, all :string columns are searched:

GET http://*.*/api/posts?search[query]=ello
# => posts with title matching "Hello" or body matching "Hello"

To search a specific attribute:

GET http://*.*/api/posts?search[query][title]=Hello
# => posts with title matching "Hello"

Fuzzy Searching

To enable fuzzy searching, first enable the pg_trgm and fuzzystrmatch extensions in a migration:

class EnablePgExtensions < ActiveRecord::Migration
  def change
    enable_extension 'fuzzystrmatch'
    enable_extension 'pg_trgm'
  end
end

Enable fuzzy searching by setting the _fuzzy flag to 1:

GET http://*.*/api/posts?search[query]=Helpi&search[_fuzzy]=1
# => posts with title matching "Hello" or body matching "Hello"

Using OR

Searching for multiple attributes using the OR attribute can be enabled by setting the _operator flag to or:

GET http://*.*/api/posts?search[query][title]=Hello&search[query][body]=World&search[_operator]=or
# => posts with title matching "Hello" or body matching "World"