我们平时使用api在进行各种操作的时候,如:query、document的增删改查等等,都会看到最后会调用.get()或者.execute().actionGet()方法。例如:

IndexResponse actionGet = transportClient
.prepareIndex(indexName, indexType)
.setSource(data)
.execute().actionGet();
return actionGet.isCreated();
DeleteResponse deleteResponse = transportClient
.prepareDelete(indexName, indexType, id)
.execute().actionGet();
return deleteResponse.isFound();
GetResponse getResponse = transportClient
.prepareGet(indexName, indexType, id)
.get();


这是由于,Elasticsearch提供的java客户端是天生异步的。


1、其中,execute() 方法中,创建了一个ActionListener,用来监听action的执行结果;然后在调用actionGet(timeout),获取最终返回结果,actionGet是一个阻塞的方法,可以设置一个超时时间,也可以不设置。


2、get(timeout)方法:

通过源码可以发现,get(timeout)方法内部就是封装了execute().actionGet(timeout)方法,其中参数timeout也是超时时间,当然也可以不设置,一致阻塞知道有返回结果。