Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integration test elastic search, timing issue, document not found

I have an integration test, in java, of a facade that does a few things, among which is an index operation into an elastic search database. This elastic search database has been very naively set up (out of the box stuff actually, I'm in the learning fase). The insertion is done, inside that facade, with the java api, also very naively, with the example nearly completely copy pasted from elastic search, as described here : http://www.elasticsearch.org/guide/reference/java-api/index_.html.

Afterwards I test a whether my facade did its stuff correctly, part of it is checking whether that document has really been inserted in the database. This I do, again, in the way elastic describes on their site : http://www.elasticsearch.org/guide/reference/java-api/search.html. I insert a document with a certain payload and look it up the same way.

This test works if I run in debug and set a breakpoint after facade did it's stuff, but it fails with no results found if I don't put this breakpoint or do not run in debug. This makes me think I'm really doing something wrong. Also, the application itself works (inserts and so on), so there's likely something wrong with my integration test, and not with my copy pasted code.

I guess that after the indexing operation returns the indexing is not really finished yet, or there is some replication going on that doesn't complete before the search, or something like that, but it eludes me what exactly and I can't seem to get it solved either. I didn't try to put elastic on one node and one shard yet, maybe there's something wrong there, but I don't really see what exactly, so I didn't walk that path yet. Like I said, just started using elastic so I might be missing something crucial and beginner-style. I can paste my exact code if needs be, but like I said it boils down to using two code snippets from the elastic search site in a test.

Kasper

like image 357
Kasper Avatar asked Oct 17 '12 13:10

Kasper


1 Answers

Elasticsearch doesn't make data available immediately after the index operation is called. It waits for 1 sec by default for more data to arrive. However, you can force elasticsearch to make all data available immediately by calling refresh:

client.admin().indices().refresh(refreshRequest()).actionGet();

Try adding this operation after your facade is done indexing before you check the final result.

like image 95
imotov Avatar answered Nov 03 '22 10:11

imotov