Elasticsearch 1.3. - Call custom REST endpoint from Java -
i building elasticsearch plugin
exposes rest
endpoint ( starting post )
i can call endpoint curl
:
curl -x post 'http://my-es:9200/lt-dev_terminology_v1/english/_terminology?pretty=-d '{ "segment": "database", "analyzer": "en_analyzer" }
my question how can call same endpoint java
, using transport client ? point me tutorial ?
i suggest take here. should starting point you.
let me sum :
considering following parameters :
string clustername = "..."; string clienttransporthost = "..."; integer clienttransportport= "..."; string clientindex = "..."; string indextype = "...";
of course replace dots settings wish use.
then define cluster settings
:
settings settings = immutablesettings.settingsbuilder() .put("cluster.name", clustername).build();
you instantiate transportclient object :
transportclient client = new transportclient(settings); client.addtransportaddress(new inetsockettransportaddress(clienttransporthost, clienttransportport));
you can verify connection using method :
private void verifyconnection(transportclient client) { immutablelist<discoverynode> nodes = client.connectednodes(); if (nodes.isempty()) { throw new elasticsearchexception( "no nodes available. verify es running!"); } else { log.info("connected nodes: " + nodes.tostring()); } }
ps: use log.info() method have instantiate logger.
so can use verification method :
verifyconnection(client);
and once you've done can per example prepare search :
searchresponse response = client.preparesearch(clientindex) .settypes(indextype) .addfields("...", "...") .setsearchtype(searchtype.default) .execute() .actionget();
ps : tested on elasticsearch 1.3
Comments
Post a Comment