Я создаю новый индекс и затем сопоставляю данные json
CreateIndexRequest request = new CreateIndexRequest("demoreport");
request.mapping(
"{\n" +
" \"properties\": {\n" +
" \"Identifier code\": {\n" +
" \"type\": \"char(3)\"\n" +
" },\n" +
" \"User ID\": {\n" +
" \"type\": \"char(38)\"\n" +
" },\n" +
" }\n" +
"}",XContentType.JSON);
HttpPost post = new HttpPost("http://localhost:9200/demoreport/_doc/5");
Мой JSON Файл:
[
{
"IDENTIFIER_CD": "PT ",
"USER_ID": "123458
}
{
"IDENTIFIER_CD": "SR ",
"USER_ID": "12345678
}
]
Я получаю сообщение об ошибке:
java .lang.IllegalArgumentException: источником отображения должны быть пары имен полей и определения свойств.
вот полный код: здесь я пытаюсь опубликовать файл Json, созданный из базы данных, в Сервер эластичного поиска.
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequest;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
public class MyScheduler {
public static void send() {
CreateIndexRequest request = new CreateIndexRequest("kibanareport");
request.settings(Settings.builder()
.put("index.number_of_shards", 20)
.put("index.number_of_replicas", 10)
);
request.mapping(
"{\n" +
" \"properties\": {\n" +
" \"user_ID\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"doc_ID\": {\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);
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.startObject();
{
builder.startObject("properties");
{
builder.startObject("message");
{
builder.field("type", "text");
}
builder.endObject();
}
builder.endObject();
}
builder.endObject();
String fileName = "downloads/JSONFile.json";
File jsonFile = new File(fileName);
HttpEntity entity = new FileEntity(jsonFile);
HttpPost post = new
HttpPost("http://localhost:9200/kibanareport/_doc");
post.setEntity(entity);
HttpClient client = new DefaultHttpClient();
post.addHeader("content-type", "application/json");
post.addHeader("Accept", "application/json");
HttpResponse response = client.execute(post);
System.out.println("Response: " + response);
}
}