meilisearch / meilisearch-rails

Meilisearch integration for Ruby on Rails
https://www.meilisearch.com
MIT License
295 stars 47 forks source link

Initial index creation and indexing #284

Closed crespire closed 1 year ago

crespire commented 1 year ago

Hey folks, new to using Meilisearch and really enjoying it so far. We are about to switch from pg_search to meilisearch-rails and I had a question about first-time setup.

I understand that Meili will auto-index via AR callbacks on any new records, but if we have existing records, it doesn't seem that Meiliesearch will index anything until I manually call reindex!.

Perhaps I have configured incorrectly?

I tried to do an after_initialization block but it is failing because the index does not yet exist? I'm not sure what the issue is:

# End of config/meilisearch.rb

Rails.configuration.after_initialize do
  unless Rails.env.test?
    Rails.logger.info 'Checking for Meilisearch index...'

    # Always attempt to create the index. Meilisearch requires indexes to be unique
    # If index already exists, this will fail silently
    client = MeiliSearch::Client.new(ENV['MEILISEARCH_HOST'], ENV['MEILISEARCH_MASTER_KEY'])
    client.create_index('Resource', primary_key: 'id')

    if Resource.index.number_of_documents.zero?
      Rails.logger.info 'Melisearch index empty, starting reindex.'
      ReindexTextSearchJob.new.perform
    else
      Rails.logger.info 'Meilisearch index populated, skipping reindex.'
    end
  end
end

I keep getting a fatal error when my CI runs assets:precompile:

-----> /home/deploy/.asdf/bin/asdf exec bundle exec rake assets:precompile
rake aborted!
MeiliSearch::ApiError: 404 Not Found - Index `PressRelease` not found.. See https://docs.meilisearch.com/errors#index_not_found.

Is there a way for me to automate a Create and index resources if an index does not exist task after initial deployment? I know Meilisearch Rails will pick up if a record is created via AR callbacks.

brunoocasali commented 1 year ago

Hi @crespire!

What is the configuration of your model? Because you're creating an index called Resource and the error shows a different name, PressRelease.

You can tweak the name of the index if you want (just take a look at the readme) :)

crespire commented 1 year ago

Sorry, I forgot to edit the error message. The model name is PressRelease but changed to Resource in sample code. That being said, I believe this particular issue was a file system persistence issue.

I'm still interested to hear about solutions for an initial index population in Rails, as that is the core problem I'm trying to solve.

brunoocasali commented 1 year ago

Ok, so first, I think you don't need the two client calls you did in the first place meilisearch-rails automatically creates the index for you.

Plus, I'm thinking about the after_initialize you opted for. You're probably loading the index somewhere else before triggering your reindex.

crespire commented 1 year ago

Hmm, I will investigate. We've pursued the solution of running an after-deploy rake task to reindex as part of our deployment process, so I'll close the issue now.

Thanks @brunoocasali, appreciate your time and assistance!