0

I am implementing Elasticsearch using Tire (and Ryan Bates' railscasts) as a site-wide search. It searches across multiple models. I would like for it to filter by current_team. I have at least two problems:

1) hard-coding the filter to 'team 2' brings back no results. In the particular query I was running, I should get two. I've tried a variety of formats of the number, the team, etc. Nothing I've tried works.

2) I don't know how to pass the team_id to the filter as a variable. I tried sending it like this: __.search(params, team), but that resulted in no query results (which led me to hard code the team id as in #1)

I've spent about 6 hours so far on this on Google. The closest thing I got to was Karmi's response to a similar question on github which basically said, 'read the manual, its there.' :) I've read it, and being a newby, I'm still lost.

Here is the code, as it stands now.

application.html.erb

<bunch of code>

<%= form_tag searches_path, method: :get do %>
  <p>
    <%= text_field_tag :query, params[:query] %>
    <%= submit_tag "Search", name: nil %>
  </p>
<% end %>

<bunch of code>

contacts_controller.rb

class Contact < ActiveRecord::Base
  attr_accessible :address_1, :address_2, :city, :first_name, :last_name, :state, :zip, :team_id
  has_and_belongs_to_many :properties
  belongs_to :user
  belongs_to :team
  has_many :appointments

  before_save :update_name 

  def update_name
    self.name = [first_name, last_name].join(' ')
  end

  #for elastic search
  include Tire::Model::Search 
  include Tire::Model::Callbacks
  def self.search(params)
    tire.search(load: true) do 
      query { string params[:query], default_operator: "AND" } if params[:query].present?
      filter :term, :team_id => ['2']
    end
  end  
end

searches_controller.rb

class SearchesController < ApplicationController

  def index
    current_team = :current_team
    @contacts = Contact.search(params)
    @properties = Property.search(params)
   # @events = Event.search(params[:query], load: true)
  end
  def show

  end

end

Search: index.html.erb:

<div id="content">
  <h1>Search Results</h1>

  <table>
  <tr>
    <th>First Name</th>
    <th>Last Name</th>
    <th>Last Name</th>
  </tr>

<% @contacts.each do |contact| %>
  <tr>
    <td><%= contact.first_name %></td>
    <td><%= contact.last_name %></td>
    <td><%= contact.team_id %></td>
  </tr>
<% end %>
</table>


  <table>
  <tr>
    <th>Name</th>
    <th>Address 1</th>
  </tr>

<% @properties.each do |property| %>
  <tr>
    <td><%= property.name %></td>
    <td><%= property.address_1 %></td>
  </tr>
<% end %>
</table>



</div>

Note that there is a similar search function in the Properties controller. I'm just trying to get Contacts to work for now.

curl command requested below produces:

Jon Lehman
  • 19
  • 6

1 Answers1

1

You can pass current_team value via params and keep the current search method signature. For example, in your controller:

def index
  params[:current_team] = current_team # method that returns current team ID, are you using devise?
  @contacts = Contact.search(params)
  @properties = Property.search(params)
  # @events = Event.search(params[:query], load: true)
end

and in your model:

def self.search(params)
  tire.search(load: true) do 
    query { string params[:query], default_operator: "AND" } if params[:query].present?
    filter :term, params[:current_team] if params[:current_team].present?
  end
end
Ahmad Sherif
  • 5,923
  • 3
  • 21
  • 27
  • I probably spent an hour yesterday (before I posted this question) trying to figure out how to put this into params. Thanks! – Jon Lehman Dec 02 '12 at 22:55