铁轨 +太阳黑子:“未分配”用于过滤未分配关联的记录的方面?
仍在学习构面,但我想过滤未分配关联记录的记录。首先是一些代码:
模型:
class Project < ActiveRecord::Base
belongs_to :category
searchable do
text :name
integer :category_id, :references => Category
end
end
控制器:
class ProjectsController < ApplicationController
def index
@search = Project.search do
fulltext params[:q]
facet :category_id
with(:category_id, params[:category_id]) if params[:category_id].present?
end
@projects = @search.results
respond_with(@projects)
end
视图:
<ul>
<% for row in @search.facet(:category_id).rows %>
<li>
<% if params[:category_id].blank? %>
<%= link_to row.instance.name, :category_id => row.value %> (<%= row.count %>)
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :category_id => nil %>)
<% end %>
</li>
<% end %>
</ul>
视图显示类别过滤器列表及其各自的计数,但是,我想包含一个“未分配”方面(及其计数)将查询尚未分配类别的项目。这对于方面来说可能吗?另外,过滤已分配任何类别的项目的“分配任何”方面(及其计数)怎么样?谢谢。
Still learning about facets, but I would like to filter for records that do not have an associated record assigned. First some code:
The model:
class Project < ActiveRecord::Base
belongs_to :category
searchable do
text :name
integer :category_id, :references => Category
end
end
The controller:
class ProjectsController < ApplicationController
def index
@search = Project.search do
fulltext params[:q]
facet :category_id
with(:category_id, params[:category_id]) if params[:category_id].present?
end
@projects = @search.results
respond_with(@projects)
end
The view:
<ul>
<% for row in @search.facet(:category_id).rows %>
<li>
<% if params[:category_id].blank? %>
<%= link_to row.instance.name, :category_id => row.value %> (<%= row.count %>)
<% else %>
<strong><%= row.value %></strong> (<%= link_to "remove", :category_id => nil %>)
<% end %>
</li>
<% end %>
</ul>
The view shows a list of category filters with their respective counts, however, I want to include an "Unassigned" facet (and its count) which will query projects that have not been assigned a category. Is this possible with facets? Also, what about an "Assigned any" facet (and its count) that filters projects that have been assigned any category? Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我知道你问这个问题已经有一段时间了。但我最近一直在使用rails/sunspot/solr,并偶然发现了你的问题。希望您已经找到了答案,但如果没有,我将按照以下方法进行操作。
分面是对当前搜索结果进行分类的一种方式。实现“未分配”方面的一种方法是实际使用它来索引对象。
在您的项目模型中,我会将其更改为:
我看到您在视图中使用
row.instance.name
来获取类别名称。因为我已将可搜索属性切换为字符串(并且出于明显的原因,您不能将:references
和row.instance
与字符串值一起使用!)我'我们选择使用category_name|category_id
类型字段的组合来索引该字段。如果项目
没有category_id
,则使用字符串“Unassigned”
。现在我们更改搜索块中的代码以反映或模型更改。
并且视图代码也发生了变化:
I know it's been a while since you asked this question. But I've recently been working with rails/sunspot/solr, and stumbled upon your question. Hopefully you've found your answer, but if not, here's how I would do it.
Facets are a way to categorize the current search results. One way to get the "Unassigned" facet implemented, is to actually index the objects with it.
In your Project Model, I would change it to:
I see you're using
row.instance.name
in the view to get the category name. Because I've switched the searchable attribute to a string (and for obvious reasons, you can't use:references
androw.instance
with a string value!) I've chosen to index the field with a combination of thecategory_name|category_id
type field. If theProject
does not have acategory_id
, the string"Unassigned"
is used.Now we change the code in your search block to reflect or model changes.
and the view code changes as well: