简体   繁体   中英

What happens to my elasticsearch index when I stop rails server?

I am very new to using Elasticsearch in a Rails application and am using the chewy gem to implement it. When I run my rails app in development mode via rails s I then run Elasticsearch via the elasticsearch command and run the rake chewy:reset:all to create an index for my data.

Everything works fine doing this but if I restart the server I have to run the rake chewy:reset:all command again to rebuild the index otherwise I get an error. What happens to the index when I restart the server? Is it destroyed when the server is stopped?

I am not very familiar with how Elasticsearch functions so would appreciate anyone shedding a little light on what is happening behind the scenes.

I have never used Chewy, but judging by their docs if you put "Chewy.settings = {prefix: 'test'}" in config/initializers/chewy.rb it will prefix everything with 'test', also in development. I don't know if that is where you put it, of course.

Note also that cucumber has a tendency to run your tests in development mode ( https://github.com/cucumber/cucumber-rails/issues/222 ).

Therefore, try removing the "Chewy.settings = {prefix: 'test'}" from your code, and instead put something like this in your chewy.yml file:

# config/chewy.yml
cucumber:
  host: 'localhost:9200'
  prefix: 'test'

And then add this to your cucumber env.rb file:

ENV["RAILS_ENV"] ||= 'cucumber'

And try running cucumber again with rake cucumber

When you run elasticsearch in your terminal, think of it as a separate server, just like your rails s . It runs completely independent from your application server. Being a Chewy user myself, I think you're dealing with polluted indexes. Here's how to troubleshoot:

Check that you are updating the index when you add/delete records to the database. If Chewy has an indexed document that has no matching record in your database, you can get some unexpected errors. According to Chewy's README :

It is also a good idea to set up the :bypass strategy inside your test suite and import objects manually only when needed, and use Chewy.massacre when needed to flush test ES indices before every example. This will allow you to minimize unnecessary ES requests and reduce overhead.

RSpec.configure do |config|
  config.before(:suite) do
    Chewy.strategy(:bypass) # if you're not using RSpec, copy this line and paste it in the setup script of your suite.
  end
end

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