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'
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)