TIL/ELK Stack

[Elasticsearch] 데이터 입력, 조회, 삭제(GET, POST, PUT, DELETE)

oraange 2023. 5. 8. 09:06

Elasticsearch 실행


Elasticsearch를 설치하는 방법은 다양하지만 나는 mac을 사용하기 때문에 homebrew를 이용하여 설치하였다.
다음 명령어로 설치를 진행하자. 참고로 나는 7.17.4 version을 설치하였다.

$ brew tap elastic/tap
$ brew install elastic/tap/elasticsearch-full

설치가 완료되면 Elasticsearch를 실행하여 보자. 참고로 Elasticsearch는 9200 포트를 사용한다.

$ elasticsearch

다음과 같이 많은 출력이 뜨고 마지막에 뭐 successfully reloaded changed 이런 멘트가 뜨면 성공적으로 실행이 된 것이다. 이제 인덱스를 조회하여보자.

GET


  • input
$ curl -XGET http://localhost:9200/classes
  • output
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [classes]",
        "resource.type" : "index_or_alias",
        "resource.id" : "classes",
        "index_uuid" : "_na_",
        "index" : "classes"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [classes]",
    "resource.type" : "index_or_alias",
    "resource.id" : "classes",
    "index_uuid" : "_na_",
    "index" : "classes"
  },
  "status" : 404
}

아직 아무 것도 건든게 없으므로 404가 나온다. http://localhost:9200/classes로 주소창에 입력해도 같은 값이 나온다.

curl -XGET http://localhost:9200/classes?pretty로 입력하면 깔끔한 결과 값을 얻을 수 있다.

PUT


  • input
$ curl -XPUT http://localhost:9200/classes
  • output
{
  "acknowledged" : true,
  "shards_acknowledged" : true,
  "index" : "classes"
}

무언가 떴다. 이제 다시 GET을 해보자.

  • input
$ curl -XGET http://localhost:9200/classes?pretty
  • output
{
  "classes" : {
    "aliases" : { },
    "mappings" : { },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "classes",
        "creation_date" : "1683380574706",
        "number_of_replicas" : "1",
        "uuid" : "ZSvRxXuARtS-b6ad_tKDqA",
        "version" : {
          "created" : "7170499"
        }
      }
    }
  }
}

정상적으로 저장되었다.

DELETE


  • input
$ curl -XDELETE http://localhost:9200/classes
  • output
{
  "acknowledged":true
}

방금 생성한 인덱스를 삭제하여보자. 그리고 다시 GET으로 확인해 보자.

  • input
$ curl -XGET http://localhost:9200/classes?pretty
  • output
{
  "error" : {
    "root_cause" : [
      {
        "type" : "index_not_found_exception",
        "reason" : "no such index [classes]",
        "resource.type" : "index_or_alias",
        "resource.id" : "classes",
        "index_uuid" : "_na_",
        "index" : "classes"
      }
    ],
    "type" : "index_not_found_exception",
    "reason" : "no such index [classes]",
    "resource.type" : "index_or_alias",
    "resource.id" : "classes",
    "index_uuid" : "_na_",
    "index" : "classes"
  },
  "status" : 404
}

정상적으로 삭제된 것을 확인할 수 있다.

POST


이번에는 도큐먼트를 생성하여 보자.

  • input
$ curl -XPOST http://localhost:9200/classes/class/1/ -d '{"title": "Algorithm", "professor": "John"}'

참고로 5.5.0 이상의 버전에서는 -H 'Content-Type: application/json' 을 추가해야 에러가 안난다.

  • output
{
  "_index":"classes",
  "_type":"class",
  "_id":"1",
  "_version":1,
  "result":"created",
  "_shards": {
    "total":2,
    "successful":1,
    "failed":0
  },
  "_seq_no":0,
  "_primary_term":1
}

이제 GET요청으로 생성한 도큐먼트를 확인하여 보자. 그 전에 인덱스를 먼저 확인해 보면,

  • input
$ curl -XGET http://localhost:9200/classes?pretty
  • output
{
  "classes" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "professor" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "title" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "classes",
        "creation_date" : "1683503584494",
        "number_of_replicas" : "1",
        "uuid" : "g6llZJ7CQ7OqJhwLpxBN9g",
        "version" : {
          "created" : "7170499"
        }
      }
    }
  }
}

index만 생성했을 때와는 구조가 조금 달라진 것을 확인할 수 있다. mappings 필드에 properties가 생겼고 그 안에 professortitle의 정보가 들어간 것을 볼 수 있다.

이번엔 생성한 도큐먼트에 대해 확인해 보자.

  • input
$ curl -XGET http://localhost:9200/classes/class/1/?pretty
  • output
{
  "_index" : "classes",
  "_type" : "class",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "title" : "Algorithm",
    "professor" : "John"
  }
}

이렇게 생성한 도큐먼트에 대해 확인할 수 있다.

반응형