14

I used following code for creating index in Elastic Search, Default JAVA API:

Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "myClusterName").put("client.transport.sniff", true).build();
Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9200));
CreateIndexRequestBuilder createIndexRequestBuilder = client.admin().indices().prepareCreate("test1");
CreateIndexResponse response = createIndexRequestBuilder.execute().actionGet();
System.out.println(response.isAcknowledged());  

REST Service:

HttpURLConnection con = null;
try
{
    String url = "http://localhost:9200/test2";

    URL resturl = new URL(url);
    con = (HttpURLConnection) resturl.openConnection();

    con.setDoOutput(true);
    con.setRequestMethod("PUT");
    BufferedReader in = null;
    try
    {
        if (con.getInputStream() != null)
        {
            in = new BufferedReader(new InputStreamReader(con.getInputStream()));
        }
    }
    catch (IOException e)
    {
        if (con.getErrorStream() != null)
        {
            in = new BufferedReader(new InputStreamReader(con.getErrorStream()));
        }
    }
    if (in == null)
    {
        throw new Exception("Unable to read response from server");
    }
    StringBuffer decodedString = new StringBuffer();
    String line;
    while ((line = in.readLine()) != null)
    {
        decodedString.append(line);
    }
    in.close();
    System.out.println("4");
    Integer responseCode = con.getResponseCode();
    System.out.println(responseCode);
}
catch (Exception ex)
{
    ex.printStackTrace();
}
finally
{
    if (con != null)
    {
        con.disconnect();
    }
}

By using REST API, I am able to create index. By default JAVA API, I am getting the following Exception.

org.elasticsearch.client.transport.NoNodeAvailableException: No node available
at org.elasticsearch.client.transport.TransportClientNodesService.execute(TransportClientNodesService.java:202)
at org.elasticsearch.client.transport.support.InternalTransportIndicesAdminClient.execute(InternalTransportIndicesAdminClient.java:85)
at org.elasticsearch.client.support.AbstractIndicesAdminClient.create(AbstractIndicesAdminClient.java:200)
at org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder.doExecute(CreateIndexRequestBuilder.java:206)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:62)
at org.elasticsearch.action.ActionRequestBuilder.execute(ActionRequestBuilder.java:57)
at ElasticSearch.createIndex(ElasticSearch.java:121)
at ElasticSearch.main(ElasticSearch.java:157)

Please guide me where I made mistake. Thanks in advance

4 Answers 4

10

The port for TransportClient(via java API) is different than Http By default, transportClient port is 9300

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

2 Comments

Thanks for your answer. Still I am getting the same exception.
It works fine now. I made a small mistake as I used 9300. But in config I updated port number.
10

With replicas and shards settings:

Settings indexSettings = ImmutableSettings.settingsBuilder()
                 .put("number_of_shards", 1)
                 .put("number_of_replicas", 1)
                 .build();
CreateIndexRequest indexRequest = new CreateIndexRequest(index, indexSettings);
client.admin().indices().create(indexRequest).actionGet();

1 Comment

Thanks :), nb : the class ImmutableSettings is removed: for the newer version, use Settings.builder(). For more info : stackoverflow.com/questions/33115504/…
6

Given that you have you client shouldn't you be able to do this then:

CreateIndexResponse createResponse = client.admin().indices().create(createIndexRequest("test1")).actionGet();

1 Comment

I would just add that the createIndexRequest method is Requests.createIndexRequest("test1")
3

Using JAVA High-Level Rest Client below code can be used to create Index.

 CreateIndexRequest request = new CreateIndexRequest("users");
        request.settings(Settings.builder()
                .put("index.number_of_shards", 3)
                .put("index.number_of_replicas", 2)
        );
        Map<String, Object> message = new HashMap<>();
        message.put("type", "text");   

        Map<String, Object> properties = new HashMap<>();
        properties.put("userId", message);
        properties.put("name", message);

        Map<String, Object> mapping = new HashMap<>();
        mapping.put("properties", properties);
        request.mapping(mapping);

        CreateIndexResponse indexResponse = client.indices().create(request, RequestOptions.DEFAULT);
        System.out.println("response id: "+indexResponse.index());

For further reference please read : https://www.elastic.co/guide/en/elasticsearch/client/java-rest/master/java-rest-high-create-index.html#java-rest-high-create-index

Comments

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.