Elasticsearch

[elasticsearch] PUT vs POST 사용법 및 차이

알로호모라 2022. 11. 7. 19:00
반응형

Elasticsearch PUT vs POST 사용법 및 차이 알아보기 

 

 

PUT, 입력

- 데이터 입력 기능 

- 데이터 수정 기능 : 존재하는 _id값이 있다면 Update 데이터를 한다. 

- 존재하는 _id값에 데이터 수정을 하기 싫다면 _create를 사용하면 에러가 낼 수 있다. 

- [ 인덱스/_doc/_id value] 형태이며 _id 값이 없으면 입력 오류가 난다. (_id 자동 생성 기능 없음 - POST는 있음) 

 

kibana로 실습해보기 

PUT test_index/_doc/1
{
  "title": "제목1",
  "content": "내용1"
}

test_index라는 Index _doc / 1 (_id 지정해주기)에 아래 내용을 입력해준다. 

 

결과 

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",  //created
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

result : created 로 나왔다. 

 

다시 똑같은 Id = 1 에 아래 내용을 한 번 더 PUT 하면 

PUT test_index/_doc/1
{
  "title": "제목1",
  "content": "내용1 수정"
}

 

Result : updated가 된다.  (여기서 주의할 점은 update가 전체 doc to doc으로 업데이트 된다. 위에서 title없이 {"content": "내용1 수정"} 만 PUT 실행하게 되면 도큐먼트 Id = 10의 도큐먼트는 {"content":"내용 1 수정"}만 나온다. 아예 도큐먼트 자체가 삭제되고 새로 덮어 씌워짐) 

  "result" : "updated",

 

 

기존 도큐먼트에 덮어씌워지는 것(update되는 것)이 싫다면 _doc 대신 _create를 사용하면 된다. 

PUT test_index/_create/1
{
  "title": "제목1",
  "content": "내용1 다시 수정"
}

_create는 같은 _id값이 있을 때 덮어씌우지 않고 에러를 낸다. 

{
  "error" : {
    "root_cause" : [
      {
        "type" : "version_conflict_engine_exception",
        "reason" : "[1]: version conflict, document already exists (current version [2])",
        "index_uuid" : "fuHzzKS8SJCqMhgKD8MxZQ",
        "shard" : "0",
        "index" : "test_index"
      }
    ],

 

POST와 가장 크게 다른 점은 PUT은 _id값을 적어주지 않으면 에러가 난다.

PUT test_index/_doc
{
  "title": "제목1",
  "content": "내용1 다시 수정"
}

결과 : PUT말고 POST를 쓰라고 알려줌 

{
  "error" : "Incorrect HTTP method for uri [/test_index/_doc?pretty=true] and method [PUT], allowed: [POST]",
  "status" : 405
}

 

 

 

 

POST, 수정 

- 데이터 입력 기능

- 데이터 수정 기능 (필드 여러개 중 원하는 필드만 바꾸기 가능 ex. indx/_update/_id)

- [index/_doc] 까지 입력하면 자동으로 _id 생성되어 에러가 나지 않음

 

POST 실습 

POST test_index/_doc/2
{
  "title": "제목2",
  "content": "내용2"
}

result : created

위의 POST 를 한 번 더 실행하면 

result : updated

까지는 PUT과 동일하다. 

POST test_index/_doc
{
  "title": "제목3",
  "content": "내용3"
}
{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "6eTMUIQBlQUEkepaP83y",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 1
}

 

도큐먼트 Id를 넣어도 넣지 않아도 자동 생성해준다.  

 

 

 

POST로 수정하기 

POST test_index/_doc/1
{
  "doc": {
    "title": "제목 수정됨 by POST"
  }
}

GET test_index/_doc/1 결과

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 7,
  "_seq_no" : 10,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "doc" : {
      "title" : "제목 수정됨 by POST"
    }
  }
}

_doc/1 로 수정하게 되면 도큐먼트 id = 1인 도큐먼트 전체를 모두 삭제하고 위와 같이 바꿔버린다. 

 

많은 필드들 중 원하는 필드만 수정하고 싶을 때 

POST test_index/_update/2
{
  "doc": {
    "title": "제목 수정됨 by POST"
  }
}

GET test_index/_update/2 결과

{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 3,
  "_seq_no" : 11,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "제목 수정됨 by POST",
    "content" : "내용2"
  }
}

수정하지않은 content가 그대로 남아있다. 

 

document를 싹 바꾸고 싶다면 

PUT index/_doc/_id 또는 POST index/_doc/_id 를 사용하고 

document의 몇몇 필드만 수정하고 나머지는 그대로 두고 싶은 경우에는

POST index/_update/_id 로 하기 ! 

 

그리고 POST 수정일 경우에는 꼭 "doc" 객체로 한 번 더 감싸주는 것 잊지않기 **  

 

 

Reference : https://esbook.kimjmin.net/04-data/4.2-crud

 

4.2 CRUD - 입력, 조회, 수정, 삭제 - Elastic 가이드북

{"_index":"my_index","_type":"_doc","_id":"1","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

esbook.kimjmin.net

 

반응형