铁轨 +太阳黑子:“未分配”用于过滤未分配关联的记录的方面?

发布于 2024-12-14 11:40:51 字数 1189 浏览 2 评论 0原文

仍在学习构面,但我想过滤未分配关联记录的记录。首先是一些代码:

模型:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

阳光下慵懒的猫 2024-12-21 11:40:51

我知道你问这个问题已经有一段时间了。但我最近一直在使用rails/sunspot/solr,并偶然发现了你的问题。希望您已经找到了答案,但如果没有,我将按照以下方法进行操作。

分面是对当前搜索结果进行分类的一种方式。实现“未分配”方面的一种方法是实际使用它来索引对象。

在您的项目模型中,我会将其更改为:

class Project < ActiveRecord::Base
  belongs_to :category

  searchable do
    text :name
    string :category_name do
      category_id.present? ? (category.name + "|" + category_id.to_s) : "Unassigned"
    end
  end
end

我看到您在视图中使用 row.instance.name 来获取类别名称。因为我已将可搜索属性切换为字符串(并且出于明显的原因,您不能将 :referencesrow.instance 与字符串值一起使用!)我'我们选择使用 category_name|category_id 类型字段的组合来索引该字段。如果项目没有category_id,则使用字符串“Unassigned”

现在我们更改搜索块中的代码以反映或模型更改。

class ProjectsController < ApplicationController
  def index
    @search = Project.search do
      fulltext params[:q]
      facet :category_name
      with(:category_name, params[:category_name]) if params[:category_name].present?
    end
    @projects = @search.results
    respond_with(@projects)
  end

并且视图代码也发生了变化:

<ul>
  <% for row in @search.facet(:category_name).rows %>
    <li>
      <% if params[:category_name].blank? %>
        <%= link_to (row.value.include? '|') ? row.value.split('|')[0] : row.value, :category_name => row.value %> (<%= row.count %>)
      <% else %>
        <strong><%= row.value %></strong> (<%= link_to "remove", :category_name => nil %>)
      <% end %>
    </li>
  <% end %>
</ul>

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:

class Project < ActiveRecord::Base
  belongs_to :category

  searchable do
    text :name
    string :category_name do
      category_id.present? ? (category.name + "|" + category_id.to_s) : "Unassigned"
    end
  end
end

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 and row.instance with a string value!) I've chosen to index the field with a combination of the category_name|category_id type field. If the Project does not have a category_id, the string "Unassigned" is used.

Now we change the code in your search block to reflect or model changes.

class ProjectsController < ApplicationController
  def index
    @search = Project.search do
      fulltext params[:q]
      facet :category_name
      with(:category_name, params[:category_name]) if params[:category_name].present?
    end
    @projects = @search.results
    respond_with(@projects)
  end

and the view code changes as well:

<ul>
  <% for row in @search.facet(:category_name).rows %>
    <li>
      <% if params[:category_name].blank? %>
        <%= link_to (row.value.include? '|') ? row.value.split('|')[0] : row.value, :category_name => row.value %> (<%= row.count %>)
      <% else %>
        <strong><%= row.value %></strong> (<%= link_to "remove", :category_name => nil %>)
      <% end %>
    </li>
  <% end %>
</ul>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文