GCP에서 Elasticsearch 사용 하기(2)

무료 크레딧 기간 중인 GCP (Google Cloud Platform) 에서 ElasticSearch Server 를 설치하고, 연습 하는 과정을 기록하는 POST 입니다.
- 잘못된 인식과 내용에 대해서는 많은 지적 부탁 합니다. -



  • 쿼리 동작 확인
$ curl  -XGET http://127.0.0.1:9200/_cluster/health?pretty
curl: (5) Could not resolve proxy: get; Unknown error


  • 상기 프록시 에러 해결을 위해 curlrc 파일을 열어 아래 내용을 추가 해 주었습니다.

$ sudo vi /etc/curlrc
proxy=http://127.0.0.1:9200

쿼리 확인
$ curl -XGET http://172.0.0.1:9200/_cluster/health?pretty
{
  "cluster_name" : "elasticsearch",
  "status" : "green",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 0,
  "active_shards" : 0,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 0,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 100.0
}

URL 뒤에 ?pretty 를 붙이지 않아도 커맨드는 실행 가능 하나, ?pretty 가 붙지 않으면 결과값이 반환 할때 한줄로 표시 되어 보기 어렵습니다.


  • 도큐멘트 작성
$ curl -XPUT 'http://127.0.0.1:9200/blog/article/1?pretty' -H 'Content-Type: application/json' -d '{"title": "New version of Elastic Search released!", "content":"This is Elastic Search","priority":1,"tags":["announce","elasticsearch","release"]}'

※6.0 버전부터 content-type의 명시가 필요 하다고 합니다.  도큐 멘트 작성/ 갱신의 경우 -H 'Content-Type: application/json' 을 입력 해 주지 않으면 에러가 발생 합니다.
{"error":"Content-Type header [application/x-www-form-urlencoded] is not supported","status":406}



  • 도큐멘트 확인
$ curl -XGET 'http://127.0.0.1:9200/blog/article/1?pretty'

만약, 존재 하지 않는 도큐멘트(ID:2) 를 취득 하려고 할 경우, 아래와 같은 에러 메세지를 반환 합니다.
$ curl -XGET 'http://localhost:9200/blog/article/2?pretty'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "2",
  "found" : false
}

  • 도큐멘트 갱신

curl -XPOST 'http://localhost:9200/blog/article/1/_update?pretty' -H 'Content-Type: application/json' -d '{"script": "ctx._source.counter= += 1"}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "remote_transport_exception",
        "reason" : "[9_frcL_][127.0.0.1:9300][indices:data/write/update[s]]"
      }
    ],
    "type" : "illegal_argument_exception",
    "reason" : "failed to execute script",
    "caused_by" : {
      "type" : "script_exception",
      "reason" : "compile error",
      "script_stack" : [
        "ctx._source.counter= +=1",
        "                                   ^---- HERE"
      ],
      "script" : "ctx._source.counter= +=1",
      "lang" : "painless",
      "caused_by" : {
        "type" : "illegal_argument_exception",
        "reason" : "invalid sequence of tokens near ['+='].",
        "caused_by" : {
          "type" : "no_viable_alt_exception",
          "reason" : null
        }
      }
    }
  },
  "status" : 400
}
※ID1의 도큐먼트에 존재 하지 않는 필드를 업데이트 하려고 하면 에러가 발생합니다.


$ curl -XPOST 'http://localhost:9200/blog/article/3/_update?pretty' -H 'Content-Type: application/json' -d '{"script": "ctx._source.counter= += 1"}'
{
  "error" : {
    "root_cause" : [
      {
        "type" : "document_missing_exception",
        "reason" : "[article][3]: document missing",
        "index_uuid" : "JA4_mLEOSimSbdXKztobaQ",
        "shard" : "4",
        "index" : "blog"
      }
    ],
    "type" : "document_missing_exception",
    "reason" : "[article][3]: document missing",
    "index_uuid" : "JA4_mLEOSimSbdXKztobaQ",
    "shard" : "4",
    "index" : "blog"
  },
  "status" : 404
}
※ID3 의 존재 하지 않는 도큐먼트를 갱신 하려고 해도 역시나 에러가 발생 합니다.


상기와 같이 존재 하지 않는 도큐먼트 혹은 필드를 에러 없이 처리 하는 방법중 하나는, upsert 리퀘스트를 주어 만약 존재 하지 않을 경우에는 신규 생성을 하도록 처리 하는 방법이 있습니다.

$ curl -XPOST 'http://localhost:9200/blog/article/3/_update?pretty' -H 'Content-Type: application/json' -d '{"script": "ctx._source.counter= +=1","upsert":{"counter":0} }'
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "3",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 1
}

※counter 필드가 존재 하지 않을면 counter 필드에 0값을 넣고, ID3인 인덱스가 생성 됩니다.

$ curl -XGET http://localhost:9200/blog/article/3?pretty
{
  "_index" : "blog",
  "_type" : "article",
  "_id" : "3",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "counter" : 0
  }
}


댓글