Insert into Elastic Search

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();

}

}

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

@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.

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){}
}

}

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.

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

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

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.