8
votes

I have been trying to put data in elastic search through java using the following code:

String url = "http://localhost:9200/testindex2/test/2";
    HttpClient client = new DefaultHttpClient();

    HttpPut put = new HttpPut(url);
    JSONObject json = new JSONObject();

    json.put("email", "[email protected]");
    json.put("first_name", "abc");
    StringEntity se = new StringEntity("JSON: " + json.toString());
    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"Text"));
    put.setEntity(se);

    HttpResponse response = client.execute(put);
    System.out.println("\nSending 'PUT' request to URL : " + url);
    System.out.println("Put parameters : " + put.getEntity());
    System.out.println("Response Code : " + response.getStatusLine().getStatusCode());

   BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

    StringBuffer result = new StringBuffer();
    String line = "";
    while ((line = rd.readLine()) != null) {
        result.append(line);
    }

    System.out.println(result.toString());

And I am getting the following error:

Sending 'PUT' request to URL : http://localhost:9200/testindex2/test/2 Put parameters : [Content-Type: text/plain; charset=ISO-8859-1,Content- Encoding: Text,Content-Length: 52,Chunked: false] Response Code : 400 {"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}},"status":400}

Also when I try the same code from a rest client it runs just fine, not sure why this problem is happening.

4

4 Answers

3
votes

Replaced

 StringEntity se = new StringEntity("JSON: " + json.toString());
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"Text"));

with this:

StringEntity se = new StringEntity(json.toString(),ContentType.APPLICATION_JSON);

and its working now

1
votes

Elastic search has special client to work with Java. And you don't need to generate JSON manually. Moreover you didn't describe import section, so a bit hard to understand what libraries you use.

0
votes

I was getting the same error, but in my case, I was actually doing something like this in a Kubernetes configuration for an init container:

- args:
        - -XPUT
        - -k
        - {{.Values.kibana.env.ELASTICSEARCH_URL}}/.logtrail/config/1?pretty
        - -H
        - 'Content-Type: application/json'
        - --data
        - /etc/logtrail/logtrail.json

The problem here is that when you specify a file in a curl POST/PUT, it needs to be appended by '@'. So the below configuration worked!

- args:
        - -XPUT
        - -k
        - {{.Values.kibana.env.ELASTICSEARCH_URL}}/.logtrail/config/1?pretty
        - -H
        - 'Content-Type: application/json'
        - --data
        - '@/etc/logtrail/logtrail.json'
0
votes
 uri = 'http://projects.local:9200/'
_index = 'mydata/'
_type = '_doc/'
head = {'Content-Type': 'application/json'}
body = {
"capital" : "boston",
"state" : "massachusetts"  
}

Using json.dumps to convert body to jbody worked for me

jbody = json.dumps(body)

response = requests.post(uri+_index+_type, headers=head, data=jbody)