0

I am trying to push the JSONFile.json into the elasticsearch. I tried using HTTP Post and Put method, but its throwing error:

Response: HTTP/1.1 405 Method Not Allowed [Allow: GET,HEAD,DELETE, content-type: application/json; charset=UTF-8, content-length: 106, access-control-allow-credentials: true]

private static void sendFile() throws Exception {
    String fileName = "downloads/JSONFile.json";
    File jsonFile = new File(fileName);
    HttpEntity entity = new FileEntity(jsonFile);

    System.out.println("here is the entity");
    System.out.println(entity);

    HttpPost post = new HttpPost("http://localhost:9200");
    post.setEntity(entity);

    HttpClient client = new DefaultHttpClient();

    //HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    //HttpClient client = clientBuilder.build();

    post.addHeader("content-type", "application/json");
    post.addHeader("Accept", "application/json");

    HttpResponse response = client.execute(post);

    System.out.println("Response: " + response);
}

I tried creating an index and an alias.

CreateIndexRequest request = new CreateIndexRequest("schools");
       request.settings(Settings.builder()
            .put("index.number_of_shards", 3)
            .put("index.number_of_replicas", 2)
    );
    request.mapping(
            "{\n" +
                    "  \"properties\": {\n" +
                    "    \"message\": {\n" +
                    "      \"type\": \"text\"\n" +
                    "    }\n" +
                    "  }\n" +
                    "}",
            XContentType.JSON);

    Map<String, Object> message = new HashMap<>();
    message.put("type", "text");
    Map<String, Object> properties = new HashMap<>();
    properties.put("message", message);
    Map<String, Object> mapping = new HashMap<>();
    mapping.put("properties", properties);
    request.mapping(String.valueOf(mapping));
    XContentBuilder builder = XContentFactory.jsonBuilder();
    builder.startObject();
    {
        builder.startObject("properties");
        {
            builder.startObject("message");
            {
                builder.field("type", "text");
            }
            builder.endObject();
        }
        builder.endObject();
    }
    builder.endObject();
    request.mapping(String.valueOf(builder));
    request.alias(new Alias("schools_alias"));
    String fileName = "downloads/JSONFile.json";
    File jsonFile = new File(fileName);
    HttpEntity entity = new FileEntity(jsonFile);
    System.out.println("here is the entity");
    System.out.println(entity);
    HttpPost post = new HttpPost("http://localhost:9200");
    post.setEntity(entity);

    HttpClient client = new DefaultHttpClient();
    //HttpClientBuilder clientBuilder = HttpClientBuilder.create();
    //HttpClient client = clientBuilder.build();
    post.addHeader("content-type", "application/json");
    post.addHeader("Accept", "application/json");
    HttpResponse response = client.execute(post);
    System.out.println("Response: " + response);
aka_code
  • 84
  • 10
  • 1
    You cannot POST and PUT to `"http://localhost:9200"` you need to specify a concrete index (or an alias that points to a single index) – Val Apr 27 '20 at 12:19
  • Hi Val, I tried that as well.. could you please give me an example.. – aka_code Apr 27 '20 at 12:34
  • What did you try? Can you show it? – Val Apr 27 '20 at 13:44
  • I tried creating an index and an alias. – aka_code Apr 27 '20 at 14:31
  • Then you need to change this: `HttpPost post = new HttpPost("http://localhost:9200/schools/_doc");` – Val Apr 27 '20 at 14:36
  • Its giving 400 error.... Response: HTTP/1.1 400 Bad Request [content-type: application/json; charset=UTF-8, content-length: 313, access-control-allow-credentials: true] – aka_code Apr 27 '20 at 14:47
  • Can you show the content of `JSONFile.json` ? – Val Apr 27 '20 at 14:47
  • [ { "name": "qwerty", "user_name": "Gkumar", "email": "gkr@yahoo.com" } ] – aka_code Apr 27 '20 at 14:55
  • You just need to remove the square brackets and it should work. Note, though, that your mapping doesn't match the content of your documents. – Val Apr 27 '20 at 15:11

1 Answers1

1

You need to change this line

HttpPost post = new HttpPost("http://localhost:9200")

To this (so that the new document lands in the right index that you just created):

HttpPost post = new HttpPost("http://localhost:9200/schools/_doc")

Also you need to remove the square brackets in JSONFile.json

Val
  • 207,596
  • 13
  • 358
  • 360
  • I need to have an array of objects in my json file. How to achieve that after removing square brackets ? – aka_code Apr 27 '20 at 18:07
  • What you need is this: https://stackoverflow.com/questions/33340153/elasticsearch-bulk-index-json-data/33340234#33340234 – Val Apr 27 '20 at 18:24