elasticsearch - How to make synchronous call in ES java API -
i using elasticsearch 2.3 (both server , client).
i added indexes using below mention code:
private transportclient txclient; // client connection code indexresponse response = txclient.prepareindex(some_values).setsource(some_value) .execute().actionget(); assert response.getid() != null;
immediately after addition, searched index using searchrequestbuilder
.
searchresponse response = txclient.preparesearch(some_values).settypes(some_value) .execute().actionget();
i did not got newly added indexes.
all want wait until indexes added. how can achieve that?
edit :
after adding .setrefresh(true)
prepareindex()
method, problem solved. far understand, it's not performance.
i did not want refresh immediately, want wait till indexes created in normal way.
you should use setwaitforyellowstatus()
method. example:
clusterhealthrequestbuilder healthrequest = txclient.admin().cluster().preparehealth(); healthrequest.setindices(newindexname); // request health of index... healthrequest.setwaitforyellowstatus(); clusterhealthresponse healthresponse = healthrequest.execute().actionget(); system.out.printf("status: %s%n", healthresponse.status()); // after checking searchresponse response = txclient.preparesearch(some_values).settypes(some_value) .execute().actionget();
i highly recommend read topic. part:
creating index asynchronous node operation executed master of cluster. cluster master node receives creation request, submits index creation participating nodes, , returns response client. meanwhile, depending on how busy other nodes are, create shards, , shards register in cluster state routing. when enough shards have registered, index state changes. operation encapsulated index-level block. unfortunately, elasticsearch not know in advance when first shard appear in routing, so, because create index call must not hang, returns early.
Comments
Post a Comment