聚合多对多关系活动记录

Aggregation on Many to many relations activerecord

我想在 rails 上对多对多 activerecord 模型进行 ES 聚合,但没有办法 我的模型:

 class Foo <  ApplicationRecord
  searchkick
  def search_data
    {
        bar: bar
    }
  end
  has_many :bars
end

我尝试了很多解决方案,但总是得到

{"active"=>{"doc_count_error_upper_bound"=>0, "sum_other_doc_count"=>0, "buckets"=>[]}}

答案是添加这个:

 def search_data
    Attributes.merge(
      bar_names: bar.map(&:name)
    )
  end

这里有更多关于如何使用关联模型的信息,这里是:

Item.rb(型号):

  # Relations
  belongs_to :brand

    ## many-to-many relations between Items and Textures
    has_many   :item_attribute_for_textures
    has_many   :textures, through: :item_attribute_for_textures, :class_name => 'ItemAttribute::Texture'


    # Searchkick
  searchkick

  def search_data
    {
      full_item_title:  full_item_title,
      brand:            brand.try(:title),
      texture:          textures.map(&:title)
    }
  end   

ItemController.rb:

  def index
    args = {}
    args[:brand]          = params[:brand]          if params[:brand].present?
    args[:texture]        = params[:texture]        if params[:texture].present?

    query = params[:busca].presence || "*"
    @items  = Item.search query, where: args, 
      aggs: { 
          full_item_title:  {},
          brand:            {},
          texture:          {}
          }
  end

并查看 index.html.erb:

<div class="row">
  <div class="col-sm-2" style="font-size: 0.75rem">

    <h5>Brands</h5>
    <div>
      <% @items.aggs["brand"]["buckets"].sort_by{ |b| b["key"] }.each do |bucket| %>
        <div>
          <% if params[:brand] == bucket["key"].to_s %>
            <strong><%= bucket["key"] %></strong>
          <% else %>
            <%= link_to bucket["key"], request.params.merge(brand: bucket["key"]) %>
          <% end %>
          (<%= bucket["doc_count"] %>)
        </div>
      <% end %>
    </div>
    <hr>

    <h5>Texture</h5>
    <div>
      <% @items.aggs["texture"]["buckets"].sort_by{ |b| b["key"] }.each do |bucket| %>
        <div>
          <% if params[:texture] == bucket["key"].to_s %>
            <strong><%= bucket["key"] %></strong>
          <% else %>
            <%= link_to bucket["key"], request.params.merge(texture: bucket["key"]) %>
          <% end %>
          (<%= bucket["doc_count"] %>)
        </div>
      <% end %>
    </div>
    <hr>

  </div>
</div>