이전 메모에는 클러스터(싱글) 헬스체크하고, 인덱스 만들어서 조회하고, 삭제하는걸 해봤는데... 정말 이걸로 끝인가(?) 흠터레스팅 하다.


bin의 .el~서치 실행해주자. -> 터미널 나갔더니 꺼져있더라. (systemd 로 나중에 등록하던가 하자.)


:thinking_face: 인덱스는 약간 RDB 테이블 같은 녀석.



  1. 약간 오버라이트 같은 데이터 수정을 해볼 것.
    앞에서 delete로 인덱스를 날렸으니 조회하면 404가 나올거다., 일단 데이터 한번 넣어서 확인 한번 해보자.

    ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "error" : {

        "root_cause" : [

          {

            "type" : "index_not_found_exception",

            "reason" : "no such index",

            "resource.type" : "index_expression",

            "resource.id" : "customer",

            "index_uuid" : "_na_",

            "index" : "customer"

          }

        ],

        "type" : "index_not_found_exception",

        "reason" : "no such index",

        "resource.type" : "index_expression",

        "resource.id" : "customer",

        "index_uuid" : "_na_",

        "index" : "customer"

      },

      "status" : 404

    }


    이렇게 404...

    ~]$ curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'

    {

      "name": "John Doe"

    }


    넣어주고.

    확인해보자.

    ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 1,

      "found" : true,

      "_source" : {

        "name" : "John Doe"

      }

    }


    음.... name 를 잘 보자. 수정은... 다음 과 같이.

    ~]$ curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'

    > {

    >   "name": "Joooooooooooooooon Duck"

    > }

    > '


    res

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 2,

      "result" : "updated",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 1,

      "_primary_term" : 1

    }



    조회 하면.

    ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 2,

      "found" : true,

      "_source" : {

        "name" : "Joooooooooooooooon Duck"

      }

    }


    존덕....으로 덮어써진 것 같다. 다른점은 version 2 가 된점.

    인덱스에 새로운 아이디(2번) 추가해보자.

    ~]$ curl -X PUT "localhost:9200/customer/_doc/2?pretty" -H 'Content-Type: application/json' -d'

    > {

    >   "name": "Jane Doe"

    > }

    > '


    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "2",

      "_version" : 1,

      "result" : "created",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 0,

      "_primary_term" : 1

    }


    얼추 드간거 같으니까. 1번 이랑 2번 한번 조회해보자. 

     ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 2,

      "found" : true,

      "_source" : {

        "name" : "Joooooooooooooooon Duck"

      }

    }

    내계정명@내서버명 ~]$ curl -X GET "localhost:9200/customer/_doc/2?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "2",

      "_version" : 1,

      "found" : true,

      "_source" : {

        "name" : "Jane Doe"

      }

    }


    1,2 볼 수 있음.

    1,2 지정해서 만들었는데, 미지정하면 알아서 autoincrement 하는 .... 것 같진 않고  지정하지 않으면 Elasticsearch에서 임의 ID를 생성하여 문서 색인화에 사용합니다. 라고 함.


    그래서 미명시 put 도 해보려고...

    앞에서 메모 한 <REST Verb> /<Index>/<Type>/<ID> 법칙엔 조금 벗어남. 


     ~]$ curl -X PUT "localhost:9200/customer/_doc/pretty" -H 'Content-Type: application/json' -d'

    {

      "name": "Jane Dodsfsfsdfsdfsdfe"

    }

    '

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


    json pretty 가 사라졌다... 아무튼 들어간거 같음.

    조회해보면

    ~]$ curl -X GET "localhost:9200/customer/_doc/pretty"

    {"_index":"customer","_type":"_doc","_id":"pretty","_version":1,"found":true,"_source":

    {

      "name": "Jane Dodsfsfsdfsdfsdfe"

    }


    이런식으로 들어 감.

    근데 이건 잘못사용한 예의 하나일 듯. (그래서 json이 이쁘게 안나온건가?)

    put의 경우 id를 명시해 주는 경우에만 사용해야하는데, 지금 res를 잘 보면 id가 pretty 가 되었음.

    가이드에 있는대로 써보면.

    미 명시의 경우 POST를 사용한다. (id자동생성)

     ~]$ curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'

    {

      "name": "Jane Doe12323456784567890"

    }
    '


    라고 날리면, res로 

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "YrQSWmQBU_p42XDzzCOy",

      "_version" : 1,

      "result" : "created",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 2,

      "_primary_term" : 1

    }


    id가 자동 생성되었다.

    아하. 그렇구나... id 지정 put , 자동지정 post..

     

  2. 정말로 수정해보자.

    update 명령어를 쓸 건데.........인덱스의 1번 id내용을 고치려고 한다.

    ~]$ curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'

    {

      "doc": { "name": "Jane Doe" }

    }

    '

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 3,

      "result" : "updated",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 3,

      "_primary_term" : 2

    }


    라고 나왔는데, ........... 어.... 버전이 2에서 3이된거보면 업데이트 된 것 같긴하고.

    한번 더 다른거 예제에 있는 내용 넣어 봤다. 나이가 추가되었네.

    ~]$ curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'

    > {

    >   "doc": { "name": "Jane Doe", "age": 20 }

    > }

    > '

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 4,

      "result" : "updated",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 4,

      "_primary_term" : 2

    }


    버전4.


    이러고 조회 한번 해보면

    ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 4,

      "found" : true,

      "_source" : {

        "name" : "Jane Doe",

        "age" : 20

      }

    }


    바뀐게 보인다.

  3. 스크립트 사용해서 변경

    나이가 숫자인데, 스크립트로 증감할 수 있나보다.

    ~]$ curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'

    > {

    >   "script" : "ctx._source.age += 5"

    > }

    > '

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 5,

      "result" : "updated",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 5,

      "_primary_term" : 2

    }


    버전이 또 올랐네.

    데이터를 조회해봄.

     ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 5,

      "found" : true,

      "_source" : {

        "name" : "Jane Doe",

        "age" : 25

      }

    }



    ... 이 방식은 1ID 도큐먼트 만 증감시켜 줬느데, 

    6.3은 query로 다중 업데이트 증가가 가능하다.

    문서는 docs-update-by-query API

    이곳을 참고하면 될 것 같다. 음.. 예제 이름이 kimchy인데, https://twitter.com/kimchy 김치형님과 관련이 있나보다.

    .... 일단 나중에 보자.

  4. 문서(doc) 삭제 방법.

    ~]$ curl -X DELETE "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "_version" : 6,

      "result" : "deleted",

      "_shards" : {

        "total" : 2,

        "successful" : 1,

        "failed" : 0

      },

      "_seq_no" : 6,

      "_primary_term" : 2

    }


    조회해보니 삭제가 되어 안보이는 것 같다.

    ~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"

    {

      "_index" : "customer",

      "_type" : "_doc",

      "_id" : "1",

      "found" : false

    }


    없다.


    일단 일도 해야하니 ㅠㅠㅠㅠㅠㅠㅠㅠㅠ 여기까지 해두고... 다음은 배치처리인가.

ref. https://www.elastic.co/guide/en/elasticsearch/reference/current/_batch_processing.html

ref. https://www.elastic.co/guide/kr/elasticsearch/reference/current/gs-modifying-data.html

+ Recent posts