1

I am having a great deal of issues with "race conditions" in my specs for Elasticsearch testing. Sometimes my tests pass, sometimes they don't, sometimes they find X number of results and sometimes they find a different number of results. This all stems from my searches being executed before ES is done indexing after new records to the DB.

I create the record, then run: Model.__elasticsearch__.refresh_index! to refresh the index and then execute my search but I am getting inconsistent search results.

I can hack my way around this by adding a sleep 3 after the .refresh_index! but this all seems like a terrible hack. Is there any other solution to this problem?

I am using the elasticsearch gem with rspec.

gem 'elasticsearch', '< 7.14'
gem 'elasticsearch-model', '7.1.1'
gem 'elasticsearch-rails', '7.1.1'
rii
  • 1,578
  • 1
  • 17
  • 22

1 Answers1

1

If I understood your workflow and use case exactly, you can leverage the refresh query parameter of the index API to make the document instantly searchable (docs)

glenacota
  • 2,314
  • 1
  • 11
  • 18
  • 1
    Yes, I saw this and also think it can help me but I am unsure on how to use it within a Ruby on Rails context? Hmm doing some research on it here is an example: https://github.com/elastic/elasticsearch-rails/issues/863 – rii Feb 27 '23 at 00:19