# Insert into Elastic Search

**URL:** <https://discuss.elastic.co/t/insert-into-elastic-search/33522>\
**Category:** Elasticsearch\
**Created:** [November 2, 2015, 2:28pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522 "2015-11-02T14:28:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![parthu](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@parthu](https://discuss.elastic.co/u/parthu)\
**Post date:** [November 2, 2015, 2:28pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/1 "2015-11-02T14:28:37Z")

</div>

While trying to insert into elastic search, this is the code I used. The code executes fine, but doesn't insert anything. When I put a debug point to see what went wrong, I get this error: **Method threw 'java.lang.StackOverflowError' exception. Cannot evaluate org.elasticsearch.common.inject.InjectorImpl.toString().**

Please note that I get to see this error only while debugging. Otherwise, the code executes fine.

Can someone please guide me through?

Code:

public class TestInsert {

```
public static void main(String[] args) {

String hostname="http://myhostname";
Client client = new TransportClient()
        .addTransportAddress(new InetSocketTransportAddress(hostname, 9300));

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

   // String json = builder.string();
    try{
        IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                        .endObject()
                )
                .execute()
                .actionGet();

    }
    catch (Exception e){}

client.close();

```

}

}

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 2, 2015, 3:48pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/2 "2015-11-02T15:48:23Z")

</div>

Hi,  
You need pass the settings, especially the cluster.name, to the constructor when creating the TransportClient. You get an NoNodeAvailableException otherwise. Rather than using the debugger, I'd suggest either looking at exceptions that are thrown (either just outputting them, or some sort of logging). Also looking at response.isCreated() in this particular case will tell you if indexing the document was successfull or not.  
Cheers

---

<div class="post-metadata">

**Author:** ![parthu](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@parthu](https://discuss.elastic.co/u/parthu)\
**Post date:** [November 2, 2015, 4:39pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/3 "2015-11-02T16:39:20Z")

</div>

@cbuescher

I did try that now. My code now looks like this. Can you tell me if I'm missing something? But still I get the same error. I attached a screenshot of the error. ![](https://us1.discourse-cdn.com/elastic/original/2X/f/f8c476e0f3da646e683983ef5969bf6d2535c5d1.JPG)

public class TestInsert {

```
public static void main(String[] args) {

String hostname="http:localhost";
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", "my-cluster-name").build();

    try{

        Client client = new TransportClient(settings)
              .addTransportAddress(new InetSocketTransportAddress(hostname, 9300));

        IndexResponse response = client.prepareIndex("twitter", "tweet", "1")
                .setSource(jsonBuilder()
                        .startObject()
                        .field("user", "kimchy")
                        .field("postDate", new Date())
                        .field("message", "trying out Elasticsearch")
                        .endObject()
                )
                .execute()
                .actionGet();
        System.out.println("Is the index created?");
        System.out.println(response.isCreated());

    }
    catch (Exception e){}
}

```

}

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 2, 2015, 4:58pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/4 "2015-11-02T16:58:26Z")

</div>

The injector you are seeing in your debugger has nothing to do with you inserting a document into Elasticsearch. It seems it doesn't like to get printed, looks like calling toString() on it creates some infinite loop, but thats an issue with your IDE.

---

<div class="post-metadata">

**Author:** ![parthu](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@parthu](https://discuss.elastic.co/u/parthu)\
**Post date:** [November 2, 2015, 5:00pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/5 "2015-11-02T17:00:11Z")

</div>

@cbuescher: Now, when I run the program, I get a NoNodeAvailableException. For the same code. This is the stack trace.

![](https://us1.discourse-cdn.com/elastic/original/2X/b/b0715b8a6a6096feaf7387c49fcf19fc5068a918.JPG)

---

<div class="post-metadata">

**Author:** ![cbuescher](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/cbuescher/32/60402_2.png) [@cbuescher](https://discuss.elastic.co/u/cbuescher)\
**Post date:** [November 2, 2015, 5:24pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/6 "2015-11-02T17:24:09Z")

</div>

Have you checked that your cluster.name really is `"my-cluster-name"`?

---

<div class="post-metadata">

**Author:** ![parthu](https://avatars.discourse-cdn.com/v4/letter/p/e5b9ba/32.png) [@parthu](https://discuss.elastic.co/u/parthu)\
**Post date:** [November 2, 2015, 6:21pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/7 "2015-11-02T18:21:41Z")

</div>

I used the "\_cluster/health?pretty=true" to see the JSON response and picked up "cluster\_name" from the response. I still don't get to know why I end up with a NoNodeAvailableException.

---

<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 5, 2017, 11:41pm UTC](https://discuss.elastic.co/t/insert-into-elastic-search/33522/8 "2017-07-05T23:41:13Z")

</div>


