# Java api fails to create index

**URL:** <https://discuss.elastic.co/t/java-api-fails-to-create-index/24030>\
**Category:** Elasticsearch\
**Created:** [June 20, 2015, 5:11am UTC](https://discuss.elastic.co/t/java-api-fails-to-create-index/24030 "2015-06-20T05:11:34Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Harish\_Kulkarni](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@Harish\_Kulkarni](https://discuss.elastic.co/u/Harish_Kulkarni)\
**Post date:** [June 20, 2015, 5:11am UTC](https://discuss.elastic.co/t/java-api-fails-to-create-index/24030/1 "2015-06-20T05:11:34Z")

</div>

I am able to add a document to an index using curl.  
But trying to add document using java api gives below error  
Primary shard is not active or isn't assigned to a known node. Timeout: [1m],

Here is my client code  
Node node = nodeBuilder().clusterName("pzap").client(true).node()  
Client client = node.client()  
I have tried without client(true) but it gives same error.

IndexRequest indexRequest = new IndexRequest("megacorp2", "Inventory", inv.id.toString());  
indexRequest.source(sb.toString());  
IndexResponse response = client.index(indexRequest).actionGet();

I have created megacorp2 index using curl

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [June 20, 2015, 5:54am UTC](https://discuss.elastic.co/t/java-api-fails-to-create-index/24030/2 "2015-06-20T05:54:08Z")

</div>

I'm sure that if you try to index a doc using curl, you will get the same error.

So the problem is not your code but your cluster.  
Check logs. Check cluster health...

---

<div class="post-metadata">

**Author:** ![jettro](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jettro/32/3418_2.png) [@jettro](https://discuss.elastic.co/u/jettro)\
**Post date:** [June 20, 2015, 6:16am UTC](https://discuss.elastic.co/t/java-api-fails-to-create-index/24030/3 "2015-06-20T06:16:01Z")

</div>

I would also check if your client is actually connected to your cluster. Is your cluster running on localhost? Does it use the default port 9300. Is the cluster name right?

You could try the cluster health from the client perspective:

```
ClusterHealthResponse clusterIndexHealths = client.admin().cluster().prepareHealth().get();
```

---

<div class="post-metadata">

**Author:** ![Harish\_Kulkarni](https://avatars.discourse-cdn.com/v4/letter/h/aeb1de/32.png) [@Harish\_Kulkarni](https://discuss.elastic.co/u/Harish_Kulkarni)\
**Post date:** [June 20, 2015, 6:17pm UTC](https://discuss.elastic.co/t/java-api-fails-to-create-index/24030/4 "2015-06-20T18:17:07Z")

</div>

This was fixed by using TransportClient.

Settings s = ImmutableSettings.settingsBuilder()  
.put("cluster.name","pzap")  
.build()  
TransportClient client = new TransportClient(s)  
client.addTransportAddress(new InetSocketTransportAddress(  
"localhost",  
9300)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 12:06am UTC](https://discuss.elastic.co/t/java-api-fails-to-create-index/24030/5 "2017-07-06T00:06:19Z")

</div>


