0

我有一个搜索字段,它从两个不同的表中查询三个不同的列:职位表中的“公司”和“行业”以及教育表中的“学校”。它成功返回所有满足输入搜索字段的所有字段的用户(使用 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,'world health组织”代表公司,“互联网”代表行业]):

在此处输入图像描述

我意识到这不是一个有效的查询,并且正在考虑加快速度的方法,但此时可以使用一些想法。

火鸡节快乐 :)

4

1 回答 1

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));

我希望它会有所帮助。

于 2012-11-22T21:37:34.900 回答