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: