Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create index in Elastic Search by Java API

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

like image 322
Ramaraj Karuppusamy Avatar asked May 20 '13 12:05

Ramaraj Karuppusamy


People also ask

How do you create an index in Java?

To create an index on a field or fields, pass an index specification document to the MongoCollection. createIndex() method. The MongoDB Java Driver provides the Indexes class that includes static factory methods to create index specification documents for the various MongoDB Index key types.

How does Elasticsearch create index?

By default, Elasticsearch has a feature that will automatically create indices. Simply pushing data into a non-existing index will cause that index to be created with mappings inferred from the data.


2 Answers

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

like image 155
nboire Avatar answered Sep 19 '22 15:09

nboire


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();
like image 23
rsilva4 Avatar answered Sep 20 '22 15:09

rsilva4