使用范围配置Sunspot Solr搜索(Configure Sunspot Solr search with scope)
调节器
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>
以上是我的控制器和型号代码。 我正在尝试设置搜索,因此它只搜索指定范围内的记录。 目前它将返回所有结果,并不注意范围。 我相信我已接近使它工作,但只是没有正确的语法。 然后我可能是错的,因此欢迎任何帮助建议或不同的方式。
Controller
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
Model
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
View
<% 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>
Above is my Controller and Model code. I am trying to set up the search so it only searches through records within the scope specified. Currently it will return all results and does not pay attention to the scope. I believe I am close to making it work but just don't have the right syntax. Then again I may be wrong, thus any help suggestions or different ways of doing this are welcome.
原文:https://stackoverflow.com/questions/29750694
最满意答案
实施下一步:
// Copy one node s, giving the copy a NULL next pointer. struct node *copy_one(struct node *s); // Add the given node s to an existing list. void add(struct node *s, struct node **existing);
这些用于许多目的,但在这里你将组成它们:
add(copy_one(s), &existing_list);
建立你的结果。
现在交叉算法是:
set result empty for all elements e in s1 if e->val is contained in s2 add a copy of e to result
对于差异
s1 - s2
,它是set result empty for all elements e in s1 if e is _not_ contained in s2 add a copy of e to result
我会让你解决这个问题。我给你一个完整的答案并不好玩。
请注意,选择链接列表来表示集合可以很好地了解C和链接列表,但通常不是最佳选择,因为它会导致大集合的性能降低。
Implement these next:
// Copy one node s, giving the copy a NULL next pointer. struct node *copy_one(struct node *s); // Add the given node s to an existing list. void add(struct node *s, struct node **existing);
These are useful for many purposes, but here you'll be composing them:
add(copy_one(s), &existing_list);
to build up your result.
Now the algorithm for intersection is:
set result empty for all elements e in s1 if e->val is contained in s2 add a copy of e to result
For difference
s1 - s2
, it'sset result empty for all elements e in s1 if e is _not_ contained in s2 add a copy of e to result
I will let you work out the C. There's no fun in me giving you a complete answer.
Note that the choice of linked lists to represent sets is fine for learning about C and linked lists, but not often the best choice because it leads to slow performance for big sets.
相关问答
更多-
对于2.4,你可以定义一个交集函数。 def intersect(*d): sets = iter(map(set, d)) result = sets.next() for s in sets: result = result.intersection(s) return result 对于较新版本的python: 交集方法采用任意数量的参数 result = set(d[0]).intersection(*d[:1]) 或者,您可以将第一组与自身相交, ...
-
我确信有一个更好的,可能是递归的方式来做到这一点,但我无法弄明白。 有任何想法吗? 我在O(n)时间构造一个反向列表,然后再次在O(n)时间检查两个列表中的配对元素。 (或者,如果您将列表存储在两个向量中,则可以通过将[mi]和b [ni]与i从1到m和n之间的最小值进行比较来开始: {1, 3, 5, 6, 4, 12} and {8, 9, 6, 4, 12} x := -1 a[5] = b[4] = 12 so x := 12 a[4] = b[3] = 4 so x := 4 a[3] ...
-
如果你想: c1 = [1, 6, 7, 10, 13, 28, 32, 41, 58, 63] c2 = [[13, 17, 18, 21, 32], [7, 11, 13, 14, 28], [1, 5, 6, 8, 15, 16]] c3 = [[13, 32], [7, 13, 28], [1,6]] 那么这里是您的Python 2解决方案: c3 = [filter(lambda x: x in c1, sublist) for sublist in c2] 在Python 3 filter返 ...
-
问题是您正在尝试访问d[my_list] - 列表不是vlaid字典键。 一种选择: set.intersection(*(set(d[k]) for k in my_list)) The problem is that you are trying to access d[my_list] – a list is not a vlaid dictionary key. One alternative: set.intersection(*(set(d[k]) for k in my_list))
-
使用* -list-to-argument运算符 ,而不是您的自定义函数使用set.intersection : >>> lists = [[1, 2, 2], [2, 3, 2], [2, 5, 2], [2, 7, 2]] >>> list(set.intersection(*map(set, lists))) [2] 如果你想要一个函数内的列表到列表的逻辑,你可以这样做: def intersect(lists): return list(set.intersection(*map(set, ...
-
实施下一步: // Copy one node s, giving the copy a NULL next pointer. struct node *copy_one(struct node *s); // Add the given node s to an existing list. void add(struct node *s, struct node **existing); 这些用于许多目的,但在这里你将组成它们: add(copy_one(s), &existing_list); ...
-
使用map将列表转换为集合并reduce以应用交集。 做: reduce(lambda x,y: x&y, map(set, hitdict.values())) 它给: set([2, 3]) Use map to convert lists to sets and reduce to apply the intersection. Do: reduce(lambda x,y: x&y, map(set, hitdict.values())) It gives: set([2, 3])
-
在C中<1>,您需要为结构指定结构节点 struct node { ... } node; 最后一个'node'是struct node类型的变量,例如 node.val = 1; 而不是一种类型。 如果你想使用'node'作为你需要写的类型 typedef struct node { .. } node; <2>如果使用void *,则需要一种机制来处理指针指向的内容,例如,如果void *指向一个整数,则需要将整数保留在堆栈或堆上。 node n; int value = ...
-
Prolog列表的交集列表(Prolog intersection list of lists)[2022-02-05]
大多数Prolog解释器已经有一个谓词来计算两个列表之间的交集: intersection/3 。 例如: ?- intersection([3,2,1], [3,9,10,4,5], R). R = [3]. 我们可以使用maplist/3来处理整行这样的列表: ?- maplist(intersection, [[1,2],[3,2,1],[3,4,5]], [[1],[3,2,1],[3,4,5]], C). C = [[1], [3, 2, 1], [3, 4, 5]]. 通过使用另一个mapl ... -
Python列表中的交集(Intersection in Python List)[2022-11-28]
鉴于交集是两个集合之间的操作,并且您已经给出了两个列表列表,因此很难清楚您正在寻找什么。 你想要[1]和b [0]的交集吗? 你想要每种可能的组合的交集吗? 我猜你想要两个列表之间的两个组合的每个组合的交集,这将是: from itertools import product [set(x).intersection(set(y)) for x, y in product(a, b)] Given that intersection is an operation between two sets, an ...