Elastic Search 05 데이터 처리:CRUD

junkyu lee·2022년 5월 12일
0

elastic_stack

목록 보기
5/7

https://esbook.kimjmin.net/04-data


Dev Tools

  • Kibana에서 dev tools 검색

  • elastic search에서 REST-API를 간단하게 사용 가능

  • kibana와 연결된 elastic 서버와 통신


    CRUD

  • elastic search에서의 도큐먼트에 접근하는 기본 url

    http://<호스트>:<포트>/<인덱스>/_doc/<도큐먼트 id>

  • dev tools를 사용하면 주소 없이 가능

PUT : create, update

  • "result" : "created"
    • request
    • response
  • "result" : "updated"
    • request
    • response
  • 이미 데이터가 있는 도큐먼트에 put을 할 경우 update됨

GET : read

  • 도큐먼트 조회
  • "_source" : 문서의 내용

DELETE : 삭제

  • 도큐먼트 또는 인덱스 단위로 삭제 가능

  • 삭제한 도큐먼트를 조회한 경우

    GET my_index/_doc/1

POST : 수정

  • post메서드는 입력에 사용 가능

    POST my_index/_doc
    {
     "name":"Jongmin Kim",
     "message":"안녕하세요 Elasticsearch"
    }
  • PUT과 달리 _doc까지 작성시 자동으로 도큐먼트id를 생성한다

  • 자동으로 생성된 id : "AfJ4toABSq1zYqySoDwU"

    _update : 수정

  • PUT으로 하는 업데이트는 필드 하나를 바꿀때도 도큐먼트 전체를 입력해야함

  • POST <인덱스>/_update/<도큐먼트 id>doc 지정자를 사용해 데이터 변경 가능

  • 새로운 도큐먼트를 PUT

PUT my_index/_doc/1
{
  "name" : "first_man",
  "messeage" : "hellow elastic search!!"
}
  • udate
POST my_index/_update/1
{
  "doc" :{
    "messeage" : "bye elastic_search!!"
  }
}

  • 조회
get my_index/_doc/1

"_version" : 2 로 증가,
_update는 전체 내용을 가져와 PUT하는 방식으로 진행됨


_bulk API

  • 여러 명령을 배치로 수행

  • elastic에는 트랜잭션 개념이 없어 롤백이나 커밋불가

  • _bulk API를 사용하여 대량의 데이터를 오버헤드없이 빠르게 수행 가능

    POST _bulk
    {명령문}
    {데이터문}
    {명령문}
    {데이터문}
    {delete문}
  • delete의 경우 데이터문이 없음

    예시)

    POST _bulk
    {"index":{"_index":"test", "_id":"1"}}
    {"field":"value one"}
    {"index":{"_index":"test", "_id":"2"}}
    {"field":"value two"}
    {"delete":{"_index":"test", "_id":"2"}}
    {"create":{"_index":"test", "_id":"3"}}
    {"field":"value three"}
    {"update":{"_index":"test", "_id":"1"}}
    {"doc":{"field":"value two"}}

    "_index" : <인덱스명>
    "_id" : <도큐먼트 id>
    {도뮤먼트 네용}

json파일을 이용한 방법

$ curl -XPOST "http://localhost:9200/_bulk" -H 'Content-Type: application/json' --data-binary @bulk.json
profile
가끔 기록하는 velog

0개의 댓글