[我正在尝试将json文件推送到elasticsearch,但是POST和PUT操作出现405错误,如何解决?

问题描述 投票:0回答:1

我正在尝试将JSONFile.json推入elasticsearch。我尝试使用HTTP Post and Put方法,但其抛出错误:

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

我尝试创建索引和别名。

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);
java json http elasticsearch post
1个回答
1
投票

您需要更改此行

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

为此(以便新文档位于您刚创建的正确索引中:]

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

此外,您需要删除JSONFile.json中的方括号

© www.soinside.com 2019 - 2024. All rights reserved.