ElasticSearch 6.4 REST-API(2) -------- 인덱스 데이터
2904 단어 ElasticSearch 관련
Client 만들기
public static RestHighLevelClient getClient(){
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost(ES_IP, ES_PORT, "http")));
return client;
}
1. 맵 형식
/**
*
* @param index
* @param type
* @param id id
* @param json
* @return
* @throws Exception
*/
public static IndexResponse index(String index,String type,String id, Map json) throws Exception{
RestHighLevelClient restClient = getClient();
IndexResponse indexResponse = null;
try {
IndexRequest indexRequest = new IndexRequest(index, type, id);
indexRequest.source(json);
indexResponse = restClient.index(indexRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}finally {
restClient.close();
}
return indexResponse;
}
2, 제이슨 형식
/**
* json
*
* @param index
* @param type
* @param id id
* @param jsonString json
* @return
* @throws Exception
*/
public static IndexResponse index(String index,String type,String id, String jsonString) throws Exception{
RestHighLevelClient restClient = getClient();
IndexResponse indexResponse = null;
try {
IndexRequest indexRequest = new IndexRequest(index, type, id);
indexRequest.source(jsonString, XContentType.JSON);
indexResponse = restClient.index(indexRequest, RequestOptions.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}finally {
restClient.close();
}
return indexResponse;
}
main 함수
public static void main(String [] args) throws Exception{
String index = "robot";
String type ="t_user";
String id = UUID.randomUUID().toString();
/* String json = "{" +
"\"name\":\"kimchy\"," +
"\"age\":\"18\"," +
"\"birth\":\"36542869\"" +
"}"; */
Map json = new HashMap<>();
jsonMap.put("name", " ");
jsonMap.put("age", 24);
jsonMap.put("birth", "564822559865");
IndexResponse indexResponse = index(index, type, id, json);
// TODO: 2018/9/9
}
데이터의 형식은 상기 2에 그치지 않습니다. indexRequest를 직접 볼 수 있습니다.source()의 실현 방법.