Manual refresh does not seem to take effect instantly

I have alot of unit tests that use an actual instance of elasticsearch. Currently between every test the index is deleted and recreated like this:

await client.indices.delete({ index });
await client.indices.create({ index, body: { settings, mappings } });

However, this is making the tests super slow. I tried to speed it up by emptying the indexes rather than recreating them between every single test, like this:

await client.indices.refresh({ index }, { request_timeout: timeout });
await client.deleteByQuery({ index, body: { query: { match_all: {} } } });
await client.indices.refresh({ index }, { request_timeout: timeout });

This is indeed much faster, but in some cases I am getting errors using this method but I don't understand why. In which situation would these two methods yield different results?

The strange thing is that for one of the tests that fails it fails at this assertion:

t.ok(result.score < 1);

It turns out that when i delete and recreate the index between tests the score in that assertion is always 0.2876821. However, when I empty and refresh the index after every test instead of recreating the index, then the scores vary. For instance, when I comment out all unit tests except the one with the above assertion, the test suddenly succeeds but the score varies between one of: 0.18232156, 0.13353139, 0.2876821 for each time i run the test. If I uncomment most tests then the above assertion fails again and the score still varies, but now ends up being >= 1. This all makes me think the refresh doesn't apply instantly and thus the searches would yield different score values depending on which updates are "visible".

Does anyone know an explanation for this? I would also love to hear suggestions on how to approach this better.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.