Skip to content

Using Storytime's Post Search

Brandon Robins edited this page Mar 31, 2015 · 2 revisions

You can search Storytime post types through search adapters available through Storytime. By default, Storytime.search_adapter will use the Postgres search adapter.

# config/initializers/storytime.rb
Storytime.configure do |config|
  # Search adapter to use for searching through Storytime Posts or
  # Post subclasses. Options for the search adapter include:
  # Storytime::PostgresSearchAdapter, Storytime::MysqlSearchAdapter,
  # Storytime::MysqlFulltextSearchAdapter, Storytime::Sqlite3SearchAdapter
  config.search_adapter = Storytime::PostgresSearchAdapter
end

If you are using a database other than Postgres be sure to change the search_adapter type in your Storytime initializer. Available search adapters include support for Postgres, MySql, MySql Fulltext (MySql v5.6.4+), and Sqlite3.

Search using Storytime.search_adapter.search()

To search all Storytime post types pass the search string to the adapter's search function:

Storytime.search_adapter.search("search terms")

To search a specific Storytime post type pass the search string along with the post type to the adapter's search function:

Storytime.search_adapter.search("search terms", Storytime::BlogPost)

Search using Storytime::PostsController

The ability to search through post types is also available in Storytime::Posts#Index.

http://localhost:3000/posts?search=search_terms

To limit the search results to a particular post type, pass the specific post type as the parameter type.

http://localhost:3000/posts?search=search_terms&type=blog_post