5

I am currently building an elasticsearch plugin which exposes a REST endpoint ( starting from this post )

I can call my endpoint with curl like this :

curl -X POST 'http://my-es:9200/lt-dev_terminology_v1/english/_terminology?pretty=-d '{ 
   "segment": "database", 
   "analyzer": "en_analyzer"
}

My question is how can I call the same endpoint from java, using a transport client ? Could you point me to some tutorial ?

0

1 Answer 1

1

I suggest that you take a look here. It should be a good starting point for you.

Let me sum up :

Considering the following parameters :

String clustername = "...";
String clientTransportHost = "...";
Integer clientTransportPort= "...";
String clientIndex = "...";
String indexType = "...";

Of course you replace the dots with the settings you wish to use.

Then you define your cluster Settings:

Settings settings = ImmutableSettings.settingsBuilder()
        .put("cluster.name", clustername).build();

You instantiate the TransportClient object :

    TransportClient client =  new TransportClient(settings);
client.addTransportAddress(new InetSocketTransportAddress(clientTransportHost, clientTransportPort));

You can verify your connection using this method :

private void verifyConnection(TransportClient client) {
    ImmutableList<DiscoveryNode> nodes = client.connectedNodes();
    if (nodes.isEmpty()) {
        throw new ElasticsearchException(
                "No nodes available. Verify ES is running!");
    } else {
        log.info("connected to nodes: " + nodes.toString());
    }
}   

PS: to use the log.info() method you have to instantiate a Logger.

So now you can use the verification method :

verifyConnection(client);

and once you've done all that you can now per example prepare a search :

SearchResponse response = client.prepareSearch(clientIndex)
            .setTypes(indexType)
                            .addFields("...", "...")
            .setSearchType(SearchType.DEFAULT)
                            .execute()
                            .actionGet();

PS : Tested on Elasticsearch 1.3

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your response. My problem isn't how to create the transport client, but how to use it to access my own endpoint. I found this great tutorial ( jprante.github.io/lessons/2012/03/27/… ) which helps but now I have no idea how to get to the client object inside the transport class because what I need is to use existing functionality ...
May I know why has it been down-voted ? At least if you don't agree with my answer, behave like a respectful adult and comment on why you have down-voted a valid answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.