模型 class 的 Searchkick 聚合

Searchkick aggregations by a model's class

我的 searchkick 实现跨多个索引进行搜索。它看起来像这样:

@results = Searchkick.search(
  params[:query],
  index_name: [Actors, Producers, Directors],
  fields: ["name"],
  indices_boost: {Actors => 4, Producers => 8, Directors => 2},
  page: params[:page],
  per_page: cookies[:per_page]
  )

如何实现聚合,以便根据 class(演员、制片人、导演)过滤结果?

我可以通过向我的模型添加以下 search_data 来解决这个问题。

def search_data
 {
   name: name,
   class_name: self.class.name
 }
end

然后更新控制器:

@results = Searchkick.search(
  params[:query],
  index_name: [Actors, Producers, Directors],
  fields: ["name"],
  indices_boost: {Actors => 4, Producers => 8, Directors => 2},
  aggs: {class_name:{}},
  page: params[:page],
  per_page: cookies[:per_page]
  )