해당 글은 인프런 Minsuk Heo 님의 ELK 강의를 듣고 정리한 내용입니다.
이전 글에서, 간단히 이미지로 SQL과 매핑되는 Elastic Search 명령어들을 알아보았습니다.
이번 포스팅에서는 좀 더 자세히 알아보겠습니다.
[CURD]
🌱 INSERT
curl -XPOST http://localhost:9200/classes
- classes 라는 index(database) 를 생성한다.
curl -XPOST http://localhost:9200/classes/class
- classes index 안에 class 라는 type(table) 을 생성한다.
curl -XPOST http://localhost:9200/classes/class/1/ -d '
{"title" : "Algorithm", "professor" : "John"}'
- classes index 안에 class 라는 type 에 1번째 id 값에 해당 Json document(row) 를 생성한다.
curl -XPOST http://localhost:9200/classes/class/1/ -d @example.json -H 'Content-Type:application/json'
- classes index 안에 class 라는 type 에 1번째 id 값에 해당 example.json 값의 document(row) 를 생성한다.
🌱 GET
curl -XGET http://localhost:9200/classes
- classes 라는 index(database) 를 조회한다.
curl -XGET http://localhost:9200/classes/class/?pretty
- classes index 안에 class 라는 type(table) 을 조회한다.
- 조회 옵션에 ?pretty 옵션을 추가하면 json 형식에 맞게 보기 좋게 조회할 수 있다.
curl -XGET http://localhost:9200/classes/class/1/?pretty
- classes index 안에 class 라는 type 에 1번째 id 값에 해당 Json document(row) 를 조회한다.
curl -XGET http://localhost:9200/classes/class/_search=points:30&pretty
- search 옵션을 통해 원하는 document 만 검색할 수 있다. points 필드 값이 30인 document 만을 조회합니다. 해당 방식은 URI 방식입니다.
curl -XGET http://localhost:9200/classes/class/_search -d '
{
"query" : {
"term" : {"points" : 30}
}
}
- search 옵션에는 위와 같은 Request Body 방식도 존재합니다. points 필드 값이 30인 document 만을 조회합니다.
🌱 UPDATE
curl -XPOST http://localhost:9200/classes/class/1/_update -d '
{"doc" : {"unit" : 1}}'
- classes index 안에 class 라는 type 에 1번째 id 값의 필드(컬럼) unit 과 값 1 을 추가한다
curl -XPOST http://localhost:9200/classes/class/1/_update -d '
{"script" : "ctx_source.unit+=5"}'
- script 옵션을 주게되면, 프로그래밍적으로 update를 처리할 수 있다.
- unit 컬럼의 값을 +5 연산을 수행해 저장한다.
🌱 DELETE
curl -XDELETE http://localhost:9200/classes
- classes 라는 index(database) 를 삭제한다.
curl -XDELETE http://localhost:9200/classes/class
- classes index 안에 class 라는 type(table) 을 삭제한다.
curl -XDELETE http://localhost:9200/classes/class/1/
- classes index 안에 class 라는 type 에 1번째 id 값에 해당 Json document(row) 를 삭제한다.
🤾🏻♂️ BULK
curl -XPOST http://localhost:9200/_bulk?pretty --data-binary @class.json -H 'Content-Type:application/json'
- class.json 은 인덱스, 타입, id 정보가 메타데이터로 나와있으며 해당하는 json 값이 들어있다.
- 메타데이터 값에 따라 여러 json 값이 bulk update 을 통해 document 들이 생성 혹은 수정된다.
- pretty 옵션을 넣어주면 역시 update 된 값을 가독성 좋게 확인할 수 있다.
'개인 공부 > ELK' 카테고리의 다른 글
[ElasticSearch] Aggregation 이란 (4) | 2023.10.22 |
---|---|
[Elastic Search] Mapping 의 중요성 (2) | 2023.10.22 |
[ElasticSearch] 기본 개념 익히기 (2) | 2023.10.22 |