2015-04-20 84 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