module Orchestrate::Rails::Tutorial::TheSearchAction

Adding search capability to the example application

Orchestrate.io search allows collections to be queried using Lucene Query Parser Syntax.

Updating up the rails routing

Add the following line to <rails-root>/config/routes.rb

get "search_results" => "films#search_results"

Adding the search_results action to the films controller.

Update <rails-root>/app/controllers/films_controller.rb, to add the search_results action:

class FilmsController < ApplicationController

  def index
    @films = Film.all
  end

  def show
    @film = Film.find params[:id]
    @comments = @film.comments
    @sequels = @film.sequels
    @spawn_of_sequels = @film.spawn_of_sequels
  end

  def new
    @film = Film.new
  end

  def create
    @film = Film.create! attributes
    redirect_to :action => 'show', :id => @film.id
  end

  def search_results
    query_str = (params[:query_str].blank?) ? '*' : params[:query_str]
    @title = "Search results for '#{query_str}'"
    @films = Film.search_results(query_str)
  end

  private
    # "The Godfather:Part IV" --> "the_godfather_part_iv"
    def generate_primary_key
      params[:film][:title].downcase.tr(': ', '_')
    end

    def attributes
      params[:film].merge(:id => generate_primary_key)
    end
end

Adding a search form to the index view.

Update <rails-root>/app/views/films/index.html.haml to include search.

%ul.films.list-group
 %li.list-group-item
  - title = "Films - Orchestrate.IO Example Application"
  = render partial: 'films/film_list', locals: { title: title, films: @films }
 %li.list-group-item
  = render partial: 'films/query', locals: { title: "Search the Films Collection" }

Now we'll add the partial view that displays the query input form.

Create <rails-root>/app/views/films/_query.html.haml

%h3.list-group-item-heading= title
%ul.list-group
 %li.list-group-item
  .row
   = form_tag search_results_path, method: :get, class: "form-signin" do
    .input-group
     %span.input-group-addon.form-control= search_field_tag :query_str, nil, size: 40, placeholder: " Lucene query string"
     %span.input-group-addon.form-control= button_tag "Click to search", name: 'search_result', url: '/search_results', class: "btn btn-default"

Adding the view for search_results

Let's display the search results.

Create <rails-root>/app/views/films/search_results.html.haml as shown:

%ul.films.list-group
 %li.list-group-item
  = render partial: 'films/film_list', locals: { title: @title, films: @films }

Try it out

Navigate to the home page and try a couple of searches.

orchestrate-rails supports search by property names , as shown in the orchestrate.io search tutorial , and by model attribute names.

Next

Creating Orchestrate.io events