프로그래밍/ElasticSearch
(ElasticSearch) JEST Data Save 방법 (Store data)
rlawlstjd007
2020. 5. 18. 21:32
들어가며
- JEST를 사용하여 Elastics Search DB 서버에 특정 데이터를 저장하는 방법에 대해서 알아보도록 하겠습니다.
실행환경
- ElasticSeacrh (V5.5.1)
- JEST (V5.3.2)
- compile group: 'io.searchbox', name: 'jest', version: '5.3.2'
구현방법
Index
클래스의 Builder를 이용하여 Object를 저장해주는 형식이다.
/**
* DB에 obj 의 정보를 저장한다. obj 에 id가 부여되어 있고, 이미 DB에 있는 id 라면 갱신한다.
*
* @param obj DB에 넣을 object
* @return 저장한 obj 의 id
*/
public String save(T obj) {
LOGGER.debug(String.format("Save %s (%s/%s) in database", classOfT.getSimpleName(), indexName, typeName));
final DocumentResult result;
try {
final Index.Builder builder = new Index.Builder(obj)
.index(indexName)
.type(typeName)
.refresh(true);
getId(obj).ifPresent(builder::id);
result = client.execute(builder.build());
} catch (Exception e) {
e.printStackTrace();
throw new ElasticOperationException("Failed to save at ElasticSearch", e);
}
if (!result.isSucceeded()) {
e.printStackTrace();
throw new ElasticOperationException(result.getErrorMessage());
}
return result.getId();
}
관련글
(ElasticSearch) ElasticSearch 프론트엔드 플러그인 elasticsearch-head 설치 및 사용법
elasticsearch-head ElasticSearch DB 정보 및 쿼리를 Front End 페이지를 통해서 조작할 수 있는 플러그인이다. 아래와 같이 생겼으며 Index리스트 및 상세 정보, Query 입력창 등을 이용할 수 있다. 설치방법..
jinseongsoft.tistory.com
반응형