Nested associations - elasticsearch-rails or chewy?
I assume you have accessible and working ElasticSearch instance. If you want to use queries with nested entities you have to use interfaces provided by chewy
gem i.e. define custom index field. Here is example from vanilla documentation. First of all you need to create /app/chewy/article_index.rb
class ArticleIndex < Chewy::Index define_type Article.published.includes(:author_name) do # `published` above is example scope of published-only articles # I guess you may have :title and :body fields in model field :title, :body # Here is custom index field with *full* author name. # We use lambda to extract it from article: field :author_name, value: ->(article) { "#{article.author.first_name} #{article.author.last_name}" } endend
Now query it as:
UsersIndex.query(text: {author_name: 'John'})# orUsersIndex.query(text: {author_name: 'Smith'})# or even ...UsersIndex.query(text: {author_name: 'John Smith'})
More readings:
chewy
gem documentation- amazing Toptal article
Cheers!