Ruby on rails 我如何让轮胎通过当前团队进行过滤?
我正在使用Tire(和Ryan Bates的railscasts)实现Elasticsearch,作为站点范围的搜索。它搜索多个模型。我希望它能被当前的团队过滤。我至少有两个问题: 1) 将筛选器硬编码为“team 2”不会返回任何结果。在我运行的特定查询中,我应该得到两个。我尝试过各种数字、团队等格式。我尝试过的都不管用 2) 我不知道如何将团队id作为变量传递给过滤器。我试着这样发送:\uuuu.search(params,team),但结果没有查询结果(这导致我将团队id硬编码为#1) 到目前为止,我已经在谷歌上花了大约6个小时。最接近我的是Karmi在github上回答了一个类似的问题,基本上是这样的:“阅读手册,它就在那里。”:)我读过,作为一个新手,我仍然迷路 这是目前的代码 application.html.erbRuby on rails 我如何让轮胎通过当前团队进行过滤?,ruby-on-rails,
elasticsearch,tire,Ruby On Rails,
elasticsearch,Tire,我正在使用Tire(和Ryan Bates的railscasts)实现Elasticsearch,作为站点范围的搜索。它搜索多个模型。我希望它能被当前的团队过滤。我至少有两个问题: 1) 将筛选器硬编码为“team 2”不会返回任何结果。在我运行的特定查询中,我应该得到两个。我尝试过各种数字、团队等格式。我尝试过的都不管用 2) 我不知道如何将团队id作为变量传递给过滤器。我试着这样发送:\uuuu.search(params,team),但结果没有查询结果(这导致我将团队id硬编码为#1) 到
<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>
联系人\u 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
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
class联系人['2']
结束
结束
结束
搜索\u 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
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
class SearchesController
搜索: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>
搜索结果
名字
姓
姓
名称
地址1
请注意,“属性”控制器中有一个类似的搜索函数。我只是想让联系人暂时工作
下面请求的curl命令生成:
您可以通过参数传递当前团队值,并保留当前搜索方法签名。 例如,在控制器中:
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
在您的模型中:
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
是否确保已将现有记录导入ES?是的…当我注释掉筛选行时,搜索字段按预期工作。请添加此命令的输出
curl-X GET“http://localhost:9200/contacts/_search“
它只是放在那里,上面有一个“>”你确定你没有忘记终端上的报价或者ES服务器正在运行吗?我昨天可能花了一个小时(在我发布这个问题之前)试图找出如何将其放入params。谢谢