elasticsearch-java api中get() 和execute().actionGet()方法

Elasticsearch提供的java客户端是天生异步的。

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

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

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