0

I want to send data to ElasticSearch using java httprequest calling _bulk api of the elasticsearch.

Reference: https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-bulk.html

This gives me this error

"Response: HttpResponseProxy{HTTP/1.1 406 Not Acceptable [content-type: application/json; charset=UTF-8] org.apache.http.client.entity.DecompressingEntity@3532ec19}"

Below is my Java Code:

package com.ElasticPublisher;

import java.io.File;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.HttpClientBuilder;
public class ElasticPublisher {
    public static void main(String args[]){
        try {
            sendFile();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    private static void sendFile() throws Exception{
        String fileName = "C:\\Users\\malin\\Documents\\ELK\\employee.json";
        File jsonFile = new File(fileName);
        HttpEntity  entity = new FileEntity(jsonFile);
        HttpPost post = new HttpPost("http://localhost:9200/_bulk");
        post.setEntity(entity);
        HttpClientBuilder clientBuilder = HttpClientBuilder.create();
        HttpClient client = clientBuilder.build();
        post.addHeader("content-type", "text/plain");
        post.addHeader("Accept","text/plain");
        HttpResponse response = client.execute(post);
        System.out.println("Response: " + response);
    }
}

*

Om Sao
  • 4,470
  • 1
  • 27
  • 45
Malinda Peiris
  • 492
  • 1
  • 6
  • 17
  • The MIME type for JSON is application/json instead of text/plain. See [here](https://stackoverflow.com/questions/477816/what-is-the-correct-json-content-type) – reiner_zuphall Jun 16 '18 at 20:56

1 Answers1

1

Your problem is most probably related to the Content-Type header you set.

Starting from Elasticsearch 6.0, all REST requests that include a body must also provide the correct content-type for that body.

Before v6.0, Elasticsearch used to guess what you might have meant in a content. To be more precise, if your body started with “{” then Elasticsearch would guess that your content was actually JSON. However, this approach was problematic and led to unexpected parsing errors sometimes. Therefore, they have come to the conclusion that "being explicit is the safer, clearer and more consistent approach".

All in all, you haven't shared the content of the entity file (employee.json). In short, you need to make sure the Content-Type is valid considering the entity content. If you are sending something different (e.g. json) than text/plain, you must consider replacing the Content-Type header with the following:

post.addHeader("Content-Type", "application/json");
kahveci
  • 1,095
  • 6
  • 19