gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:18:15 25 4
gpt4 key购买 nike

我正在尝试将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);

最佳答案

您需要更改此行

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

为此(以便新文档位于您刚创建的正确索引中):
HttpPost post = new HttpPost("http://localhost:9200/schools/_doc")

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

关于java - 我正在尝试将json文件推送到elasticsearch,但POST和PUT操作却出现405错误,如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61458743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com