Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Robust search with Searchkick

Robust search with Searchkick

Minitalk about Searchkick gem

Avatar for Nando Sousa

Nando Sousa

May 16, 2015
Tweet

More Decks by Nando Sousa

Other Decks in Technology

Transcript

  1. • Full Stack Developer at Resultados Digitais • @nandosousafr on

    twitter • Marketing B.I team • shipit.resultadosdigitais.com.br
  2. $ bundle exec rake searchkick:reindex:all Indexing $ bundle exec rake

    searchkick:reindex CLASS=Episode Episode.reindex
  3. class SearchController < ApplicationController def index end end @episodes =

    Episode.search term, includes: :podcast term = params[:q].presence || '*' eager loading app/controllers/search_controller.rb
  4. app/controllers/search_controller.rb class SearchController < ApplicationController def index term = params[:q].presence

    || '*' @episodes = Episode.search term, search_options end protected def search_options end end { includes: :podcast, highlight: { tag: '<strong>', fields: { show_notes: { fragment_size: 300 } } } }
  5. def search_options { includes: :podcast, highlight: { tag: '<strong>', fields:

    { show_notes: { fragment_size: 300 } } }, } end Boost and sort fields: ['title^10', 'show_notes^5'], order: { _score: :desc, release_date: :desc }
  6. Asynchronous reindex class Episode < ActiveRecord::Base # ActiveJob required searchkick

    callbacks: :async # Disable callbacks searchkick callbacks: false end
  7. Personalized Results class Episode < ActiveRecord::Base has_many :users def search_data

    attributes.merge( favorited_by: users.pluck(:id) ) end end # SearchController Episode.search 'Rails', boost_where: { favorited_by: current_user.id }