2015-04-20 288 views
1

控制器与范围配置太阳黑子Solr的搜索

class GearsController < ApplicationController 
    before_action :set_gear, only: [:show, :edit, :update, :destroy] 

    def primary 
    @gears = Gear.search do 
     with(:Lab, 'Primary') 
     order_by(:RU, :desc) 
    end 
    end 

型号

class Gear < ActiveRecord::Base 
    validates_uniqueness_of :Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN, :allow_blank => true 
    attr_readonly :Devtype, :Nic1mac, :Nic2mac, :Nic3mac, :IBmac, :SN 

    scope :Mini, -> { where(lab: 'Mini') } 
    scope :Primary, -> { where(lab: 'Primary') } 
    scope :Support, -> { where(lab: 'Support') } 
    scope :M5, -> { where(lab: 'M5') } 
    scope :P5, -> { where(lab: 'P5') } 

    searchable do 
    integer :RU 
    text :Devname 
    text :Devtype 
    string :Lab 
    text :Swportib 
    text :Swport1 
    text :Swport2 
    text :Swport3 
    text :IBip 
    text :Nic1 
    text :Nic2 
    text :Nic3 
    text :Nic1mac 
    text :Nic2mac 
    text :Nic3mac 
    text :IBmac 
    text :Psu1 
    text :Psu2 
    text :SN 
end 
end 

查看

<% provide(:title, "SP4 Primary") %> 
<p id="notice"><%= notice %></p> 

<h1>SP4 Primary</h1> 

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

<head> 
<style> 
table, th, td { border: 1px solid black;} 
th, td { padding: 15px;} 
</style> 
</head> 

<table> 
    <thead> 
    <tr> 
     <th>RU</th> 
     <th>Dev Name</th> 
     <th>Dev Type</th> 
     <th>Lab</th> 
     <th>SW PortIB</th> 
     <th>SW Port1</th> 
     <th>SW Port2</th> 
     <th>SW Port3</th> 
     <th>IB IP</th> 
     <th>NIC1</th> 
     <th>NIC2</th> 
     <th>NIC3</th> 
     <th>NIC1 Mac</th> 
     <th>NIC2 Mac</th> 
     <th>NIC3 Mac</th> 
     <th>IB Mac</th> 
     <th>PSU1</th> 
     <th>PSU2</th> 
     <th>SN</th> 
     <th colspan="3"></th> 
    </tr> 
    </thead> 
<tbody> 
    <% @gears.each do |gear| %> 
     <tr> 
     <td><%= gear.RU %></td> 
     <td><%= gear.Devname %></td> 
     <td><%= gear.Devtype %></td> 
     <td><%= gear.Lab %></td> 
     <td><%= gear.Swportib %></td> 
     <td><%= gear.Swport1 %></td> 
     <td><%= gear.Swport2 %></td> 
     <td><%= gear.Swport3 %></td> 
     <td><%= gear.IBip %></td> 
     <td><%= gear.Nic1 %></td> 
     <td><%= gear.Nic2 %></td> 
     <td><%= gear.Nic3 %></td> 
     <td><%= gear.Nic1mac %></td> 
     <td><%= gear.Nic2mac %></td> 
     <td><%= gear.Nic3mac %></td> 
     <td><%= gear.IBmac %></td> 
     <td><%= gear.Psu1 %></td> 
     <td><%= gear.Psu2 %></td> 
     <td><%= gear.SN %></td> 
     <td><%= link_to 'Show', gear %></td> 
     <td><%= link_to 'Edit', edit_gear_path(gear) %></td> 
     <td><%= link_to 'Destroy', gear, method: :delete, data: { confirm: 'Are you sure?' } %></td> 
     </tr> 
    <% end %> 
    </tbody> 
</table> 

<br> 

以上是我的控制器和型号代码。我正在尝试设置搜索,以便仅在指定的范围内搜索记录。目前它将返回所有结果并且不关注范围。我相信我接近于让它工作,但没有正确的语法。然后,我可能又错了,因此欢迎任何帮助建议或不同的方式。

回答

0

溶液加入without我gears_controller.rb文件。

gears_controller.rb

def rack1 
    @gears = Gear.where("Lab like ? OR Lab like ? OR Lab like ?", "%Primary%", "%Mini%", "%Support%") 
    @search = Gear.search do 
     fulltext params[:search] 
     without(:Lab, "P5") 
     without(:Lab, "M5") 
     order_by(:RU, :desc) 
    end 
    @gears = @search.results 
    end 

添加无场让我只有通过我的@gears变量中指定的特定实验室值的记录进行搜索。

0

你可以在你的搜索方法

with(:lab, 'Support') 

添加像这样的代码,并在模型中添加此

string :lab 
+0

我试过你的建议@罗克西,但仍然没有运气。我继续收到所有结果,而不仅仅是范围内的结果。 – aealvarez3

+1

你必须杀死你的服务器并运行耙太阳黑子:solr:reindex – rocki

+0

@ aealvarez3如果它已经为你工作,请接受答案 – rocki