toptal / chewy

High-level Elasticsearch Ruby framework based on the official elasticsearch-ruby client
MIT License
1.88k stars 364 forks source link

Update Index Derived from Multiple Associated Models #918

Open jvanderen1 opened 8 months ago

jvanderen1 commented 8 months ago

As a developer, I would like to see an example of an index which is derived from multiple Ruby models. Based on the documentation today, I only see indices with a single index_scope defined. I am actually not sure if it is possible today, but I would like to update my Elasticsearch documents multiple-model associations:

class Employee < ActiveRecord::Base
  # TODO: How to update specific `restaurant` document(s)?
  # update_index('restaurant') { ??? }
end

class Cuisine < ActiveRecord::Base
  # TODO: How to update specific `restaurant` document(s)?
  # update_index('restaurant') { ??? } 
end

class Restaurant < ActiveRecord::Base
  has_many :employees
  has_many :cuisines

  update_index('restaurant') { self }
end

class RestaurantsIndex < Chewy::Index
  index_scope Restaurant
end

In this contrived example, a Restaurant would have a foreign key relationship with Employee and Cuisine, but an Employee nor a Cuisine do not necessarily belong to a Restaurant. Therefore, when the associated records update, the RestaurantsIndex would miss these updates, correct? How can we ensure our index is always up-to-date with our DB record associations?

I did come across https://github.com/toptal/chewy/issues/34 before making this issue. However, this seems to reference an older syntax / version of Chewy.