ElasticSearch Java API:客户端创建

时间:2017-04-18 13:01:07

标签: java elasticsearch elasticsearch-5

我们是否必须为每个操作创建一个Client对象,或者在应用程序的生命周期中使用唯一的对象进行所有操作?

1 个答案:

答案 0 :(得分:2)

您可以实例化像documentation中所述的TransportClient:

TransportClient client = new PreBuiltTransportClient(Settings.EMPTY)
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host1"), 9300))
        .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("host2"), 9300));

此客户端实例继承AbstractClient

中的所有方法
admin, bulk, bulk, clearScroll, clearScroll, count, count, delete, 
delete, deleteIndexedScript, deleteIndexedScript, execute, execute, exists,
exists, explain, explain, fieldStats, fieldStats, get, get,
getIndexedScript, getIndexedScript, headers, index, index, multiGet, multiGet, multiPercolate, multiPercolate, multiSearch, multiSearch,
multiTermVectors, multiTermVectors, percolate, percolate, prepareBulk,
prepareClearScroll, prepareCount, prepareDelete, prepareDelete,
prepareDeleteIndexedScript, prepareDeleteIndexedScript, prepareExecute,
prepareExists, prepareExplain, prepareFieldStats, prepareGet,
prepareGet, prepareGetIndexedScript, prepareGetIndexedScript,
prepareIndex, prepareIndex, prepareIndex, prepareMultiGet,
prepareMultiPercolate, prepareMultiSearch, prepareMultiTermVectors,
preparePercolate, preparePutIndexedScript, preparePutIndexedScript,
prepareSearch, prepareSearchScroll, prepareSuggest, prepareTermVector,
prepareTermVector, prepareTermVectors, prepareTermVectors,
prepareUpdate, prepareUpdate, putIndexedScript, putIndexedScript,
search, search, searchScroll, searchScroll, settings, suggest, suggest,
termVector, termVector, termVectors, termVectors, threadPool, update,
update

所以回答你的问题;您可以多次重复使用同一个客户端对象。