1

Prerequisite:

  • Elasticsearch is up and running
  • Database is already filled with data
  • Rails models look like

    class Customer < ActiveRecord::Base
      include Elasticsearch::Model
      include Elasticsearch::Model::Callbacks
    end
    

Question: What is the correct way to index the models?

My assumption:

  1. Execute the following rake task for every model I want to add to elasticsearch, but only the very first time

bundle exec rake environment elasticsearch:import:model CLASS='Customer'

  1. I add "Customer.import" to the very bottom of the models file to insure that the models get indexed every time I start my rails application.

Is that enough? Will this work, even if I change the mappings of the model or if I add a new field?

1 Answer 1

3

Adding elasticsearch do existing model:

  • First adding the callbacks module will take care of indexing any new records or updating existing records you have already added it include Elasticsearch::Model::Callbacks

  • Second, you need to specify which fields are you going to index in that model by defining the following method at your model as_indexed_json

    def as_indexed_json(options={}) as_json( only: [:first_name, :last_name, :address], include: [products: { only: :name }] ) end

    This is a very good article explainging everything and of course everytime you need to index a new field, you need to updated the as_indexed_json method and also run the rake task. to rebuild the indexes agaain.

  • Third to index the data you have it already before installing elasticsearch you can just create a rake task to to call import method on that model to re-index or create a new index for that model.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.