我有一个搜索字段,它从两个不同的表中查询三个不同的列:来自职位表的“公司”和“行业”以及来自教育表的“学校”。它成功返回满足输入搜索字段的所有字段的所有用户(使用select_tag)。这是我的观点:
<%= form_tag(search_path, :method => :get, :id => "people_search") do %>
<div class="row-fluid">
<div class="span4">
<table>
<tr>
<td>
<div class="search-table" style="padding-left:55px">
<%= select_tag "all", options_for_select((@visible_companies + @visible_industries + @visible_schools).uniq, params[:all]), { :placeholder => "Search by companies, industries and schools...", :multiple => true, :js => "if (evt.keyCode == 13) {form.submit();}" } %>
</div>
</td>
<td>
<%= submit_tag "Add", id: "send-button", style:"width:175px;" %>
</td>
</tr>
</table>
</div>
<% end %>
</div>
和控制器:
@visible_positions = Position.where{ is_visible.eq('true') }
@visible_educations = Education.where{ is_visible.eq('true') }
@visible_companies = @visible_positions.order("LOWER(company)").map(&:company).uniq
@visible_industries = @visible_positions.order("LOWER(industry)").map(&:industry).uniq
@visible_schools = @visible_educations.order("LOWER(school)").map(&:school).uniq
@c = @visible_positions.where{company.in(my{params[:all]})}.map(&:user_id)
@i = @visible_positions.where{industry.in(my{params[:all]})}.map(&:user_id)
@s = @visible_educations.where{school.in(my{params[:all]})}.map(&:user_id)
@C = @visible_positions.where{company.in(my{params[:all]})}.map(&:company)
@I = @visible_positions.where{industry.in(my{params[:all]})}.map(&:industry)
@S = @visible_educations.where{school.in(my{params[:all]})}.map(&:school)
@blacklist = []
@cis = @c + @i + @s
@experiences = ([@C,@I,@S].reject(&:empty?).reduce(:&))
@cis.uniq.each do |user_id|
unless @C.empty?
@C.uniq.each do |company|
unless Position.find_all_by_company(company).map(&:user_id).include?(user_id) || Position.find_all_by_industry(company).map(&:user_id).include?(user_id) || Education.find_all_by_school(company).map(&:user_id).include?(user_id)
@blacklist << user_id
end
end
end
unless @I.empty?
@I.uniq.each do |industry|
unless Position.find_all_by_industry(industry).map(&:user_id).include?(user_id) || Position.find_all_by_company(industry).map(&:user_id).include?(user_id) || Education.find_all_by_school(industry).map(&:user_id).include?(user_id)
@blacklist << user_id
end
end
end
unless @S.empty?
@S.each do |school|
unless Education.find_all_by_school(school).map(&:user_id).include?(user_id) || Position.find_all_by_company(school).map(&:user_id).include?(user_id) || Position.find_all_by_industry(school).map(&:user_id).include?(user_id)
@blacklist << user_id
end
end
end
end
unless @c.empty? && @i.empty? && @s.empty?
@users = User.find(@cis - @blacklist)
end
搜索看起来像这样(注意单个字段),包含一个示例查询(注意AND过滤器...我是数据库中唯一适合所有搜索词的用户['dartmouth college'for school,公司的“世界卫生组织”,“工业互联网”):
我意识到这不是一个有效的查询,我正在考虑如何加快速度,但此时可能会使用一些想法。
快乐的火鸡日:)
答案 0 :(得分:1)
根据您的描述而不是理解您的代码,我发现了类似的内容
User.joins(:positions, :educations).where("lower(positions.company) like lower(?) and lower(positions.industry) like lower(?) and lower(educations.school) like lower(?) and positions.is_visible and educations.is_visible", "%#{company}%", "%#{industry}%", "%#{school}%")
或者栏目中只有一家公司或行业
User.joins(:positions, :educations).where("(lower(positions.company) = lower(?) or lower(positions.industry) = lower(?)) and lower(educations.school) = lower(?) and positions.is_visible and educations.is_visible", company,industry, school)
但是,将许多行业,公司,学校作为参数更加复杂 并创建索引
create index positions_lower_company on positions (lower(company));
create index positions_lower_industry on positions (lower(industry));
create index educations_lower_school on educations (lower(school));
我希望它会以某种方式提供帮助。