elasticsearch,searchkick,Ruby On Rails,elasticsearch,Searchkick" /> elasticsearch,searchkick,Ruby On Rails,elasticsearch,Searchkick" />

Ruby on rails 多对多关系activerecord上的聚合

Ruby on rails 多对多关系activerecord上的聚合,ruby-on-rails,elasticsearch,searchkick,Ruby On Rails,elasticsearch,Searchkick,我想在rails上的多对多activerecord模型上进行ES聚合,但没有办法 我的模型: class Foo < ApplicationRecord searchkick def search_data { bar: bar } end has_many :bars end 答案是加上: def search_data Attributes.merge( bar_names: bar.map(&:nam

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

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

答案是加上:

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

有关如何使用关联模型的更多信息,请参见:

Item.rb(型号):

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>

品牌

()

纹理 ()

请改进您的问题。第一件事:你不应该在记录中这样做。第二:
有很多:条
。嘿@ARK我得到解决方案了,检查一下
  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
<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>