简体   繁体   中英

Searchkick aggregations by a model's class

My searchkick implementation searches across multiple indexes. It looks like this:

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

How can I implement aggregations so I can filter the results based on class (actors, producers, directors)?

I was able to fix this by adding the following search_data to my model.

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

And then updated the controller with:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM