我有一个类别方面挂钩到文章模型中,但是当我在类别模型中更改类别名称时,我无法弄清楚如何更新类别名称



问题:

  • 当我转到category/edit/1并更改类别名称时,旧名称仍然显示在facet输出中
  • 当我创建一个新的类别时,它不会显示在我的文章中。类别方面,直到我创建了至少一篇具有该类别名称的文章(有道理,但不是故意的)

以下是文章模型的相关部分:

class Article < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks
  # This is just a custom module I made that does index.refresh on save/delete
  # to ensure that I'm getting a fresh view of the index on a page load.
  include ElasticSearch::RefreshIndex
  # Trimmed out a bunch of fields, but this should be enough to see what's going on.
  mapping do
    indexes :title, type: "string", analyzer: "snowball", boost: 10
    indexes :category do
      indexes :id, type: "integer", index: "not_analyzed", include_in_all: false
      indexes :name, type: "string", index: "not_analyzed"
    end
  end
  belongs_to :category, touch: true
  def self.search()
    # Trimmed out a lot of code here, but this is just the facet.
    facet "categories" do
      terms "category.name", all_terms: true
    end  
  end
  def to_indexed_json
    to_json( include: { category: { only: [:id, :name] } } )
  end
end

以下是类别模型的相关部分:

class Category < ActiveRecord::Base
  include Tire::Model::Search
  include Tire::Model::Callbacks
  include ElasticSearch::RefreshIndex
  mapping do
    indexes :id, type: "integer", index: "not_analyzed", include_in_all: false
    indexes :name, type: "string", analyzer: "not_analyzed", boost: 50
    indexes :description, type: "string", analyzer: "snowball"
  end
  has_many :articles, dependent: :destroy
  after_touch() { tire.update_index }
end

以下是我如何在文章/索引视图中使用categories方面:

<% @articles.facets["categories"]["terms"].each do |facet| %>
  <%= facet_item "checkbox", "category_name[]",
      option_value: facet["term"],
      option_text: facet["term"],
      facet_count: facet["count"],
      param: "category_name"
  %>
<% end %>

不要担心facet_item的作用,它只是一个视图帮助程序,可以正确地呈现复选框、标记、计数并设置正确的选中状态。

我需要做些什么来解决上面提到的2个问题?第一个对我来说是最重要的,但理想情况下,我很想弄清楚如何让两者都发挥作用。

您只需在编辑所述类别

后,强制对使用您刚刚编辑的类别的每一篇文章进行重新索引

最新更新