按模型类的searchkick聚合

3wabscal  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(433)

我的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]
  )

如何实现聚合,以便根据类(参与者、生产者、导演)过滤结果?

4si2a6ki

4si2a6ki1#

我可以通过在我的模型中添加以下搜索数据来解决这个问题。

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]
  )

相关问题