具有has_many关联属性的Searchkick / Elasticsearch聚合

时间:2020-05-17 19:44:42

标签: elasticsearch searchkick

我有一个与典型的“订单”模型相似的模型:

# == Schema Information
#
# Table name: orders
#
#  id             :bigint           not null, primary key
#  date       :datetime
class Order < ApplicationRecord
   has _many :order_items
end
# == Schema Information
#
# Table name: order_items
#
#  id             :bigint           not null, primary key
#  order_id             :bigint
#  category       :string
class OrderItem < ApplicationRecord
   belongs_to :orders
end

因此,想象一下带有各种order_item水果,肉,蔬菜等的各种订单。

我已经知道如何使用aggs构建一些基本的过滤器,等等:

@orders = Order.search("*", aggs: { date: {...} )

我知道我可以添加类似的内容来搜索相关的order_items:

  def search_data
    attributes.merge({
    item_category: order_items.map(&:category).join(" ")
      })
  end

我迷失的是,如何从我的文档/搜索中不清楚的是如何为此添加agg,即我想看到包含水果,肉类,蔬菜等的order_items的订单数量。?

0 个答案:

没有答案
相关问题