Elasticsearch 살펴보기 2탄 - Document API

bradley·2022년 6월 13일
1

ELK

목록 보기
4/8

Elasticsearch는 단일 문서(single document) APIs와 다중 문서(multi-document) API를 제공한다.

Index API

Index API는 특정 매핑을 가지는 index에 대한 요청이 있을 때 index에 JSON document를 추가하거나 업데이트하는데 도움을 준다.

Comman Example
아래 request는 schools 및 그에 대한 mapping을 index에 JSON Object로 추가한다.

PUT schools/_doc/5
{
   "name":"City School", 
   "description":"ICSE", 
   "street":"West End",
   "city":"Meerut",
   "state":"UP", 
   "zip":"250002", 
   "location":[28.9926174, 77.692485],
   "fees":3500,
   "tags":["fully computerized"], 
   "rating":"4.5"
}

정상 출력

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

자동 Index 생성

특정 index에 JSON Obejct를 추가하는 요청이 있을 때 index가 존재하지 않으면 API는 자동으로 index와 그에 대한 mapping을 생성한다.

자동 index 생성 비활성화

elasticsearch.yml 파일에서 아래 파라미터 값으로 이 기능을 비활성화할 수도 있다.

action.auto_create_index:false
index.mapper.dynamic:false

자동 index 생성 제한하기

다음과 같이 index 이름에 패턴을 줌으로서 자동 index 생성에 제한을 줄 수 있다.

action.auto_create_index:+acc*,-bank*
  • + : 허용
  • - : 거부
    acc*로 시작하는 index는 자동 index 생성을 하고, bank로 시작하는 index는 자동 생성을 비활성화한다.

버전 관리(Versioning)

Elasticsearch는 version 제어 기능도 제공한다. 특정 document의 version을 명세하는 query parameter를 사용할 수 있다.

Command Example

PUT schools/_doc/5?version=7&version_type=external
{
   "name":"Central School", 
   "description":"CBSE Affiliation", 
   "street":"Nagan",
   "city":"paprola", 
   "state":"HP", 
   "zip":"176115", 
   "location":[31.8955385, 76.8380405],
   "fees":2200, 
   "tags":["Senior Secondary", "beautiful campus"], 
   "rating":"3.3"
}

정상 출력

{
  "_index" : "schools",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 7,
  "result" : "updated",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 3,
  "_primary_term" : 1
}

버전 관리는 실시간 프로세스이며 실시간 검색 작업의 영향을 받지 않는다.
버전 관리에는 내부 버전관리, 외부 버전관리 중요한 두 가지 유형이 있다.

내부 버전 관리

내부 버전 관리(Internal versioning)는 1로 시작하고 업데이트 및 삭제가 일어날 때마다 증가하는 기본 버전이다.

외부 버전 관리

documents의 버전관리가 3rd party 버전관리같은 외부 시스템에 저장될 때 사용된다. 이 기능을 사용하려면 version_type을 external로 설정해야 한다. 여기서 Elasticsearch는 외부 시스템에서 지정한 대로 버전 번호로 저장하고 자동으로 증가시키지 않는다.

작업 유형(Operation Type)

작업 유형(op_type)은 생성 작업을 강제할 때 사용된다. 이것은 기존 document를 덮어쓰는 것을 방지할 수 있다.

Command Example

PUT chapter/_doc/1?op_type=create
{
   "Text":"this is chapter one"
}

정상 출력

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

자동 ID 생성

ID를 index 작업에 명세하지 않으면 Elasticsearch는 자동으로 해당 document의 id를 생성한다.

Command Example

POST chapter/_doc/
{
   "user" : "tpoint",
   "post_date" : "2018-12-25T14:12:12",
   "message" : "Elasticsearch Tutorial"
}

정상 출력
해당 문서에 대한 _id가 랜덤한 값으로 자동 생성된 것을 확인할 수 있다.

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

Get API

Get API는 특정 document에 get request를 수행함으로서 JSON Object type을 추출하는데 도움을 준다.

  • 이 작업은 실시간이며, index의 새로고침 빈도에 영향을 받지 않는다.
  • version을 명시할 수 있고 Elasticsearch는 그 버전의 document만 가져온다.
  • request에 _all을 명시할 수 있다. 그러면 Elasticsearch는 모든 type의 document id를 검색하고, 첫 번째 매치되는 document를 return한다.
  • 특정 document로부터 원하는 fields만 명세할 수 있다.

Command Example

GET schools/_doc/5

정상 출력

{
  "_index" : "schools",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 7,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Central School",
    "description" : "CBSE Affiliation",
    "street" : "Nagan",
    "city" : "paprola",
    "state" : "HP",
    "zip" : "176115",
    "location" : [31.8955385,76.8380405],
    "fees" : 2200,
    "tags" : ["Senior Secondary","beautiful campus"],
    "rating" : "3.3"
  }
}

특정 필드만 가져오기

Command Example
_source에서 namefees field만 가져온다.

GET schools/_doc/5?_source_includes=name,fees

정상 출력

{
  "_index" : "schools",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 7,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "fees" : 2200,
    "name" : "Central School"
  }
}

_source 부분만 가져오기

Command Example

GET schools/_doc/5?_source

정상 출력

{
  "_index" : "schools",
  "_type" : "_doc",
  "_id" : "5",
  "_version" : 7,
  "_seq_no" : 3,
  "_primary_term" : 1,
  "found" : true,
  "_source" : {
    "name" : "Central School",
    "description" : "CBSE Affiliation",
    "street" : "Nagan",
    "city" : "paprola",
    "state" : "HP",
    "zip" : "176115",
    "location" : [31.8955385,76.8380405],
    "fees" : 2200,
    "tags" : ["Senior Secondary","beautiful campus"],
    "rating" : "3.3"
  }
}

refresh parameter를 true로 설정함으로서 get 작업 전에 shard를 refresh 할 수도 있다.

Delete API

HTTP DELETE request를 Elasticsearch에 보냄으로서 특정 index, mapping 또는 document를 삭제할 수 있다.

Command Example

DELETE schools/_doc/4

정상 출력

{
	"found":true, 
    "_index":"schools", 
    "_type":"school", 
    "_id":"4", 
    "_version":2,
	"_shards":{
    	"total":2, 
        "successful":1, 
        "failed":0
	}
}
  • 특정 version을 삭제하기 위해 document의 version도 명세할 수 있다.
  • Routing parameter는 특정 유저의 document를 삭제하며, document가 해당 특정 유저에 속하지 않으면 작업이 실패한다. 이 작업 중에 GET API와 동일하게 refresh 및 timeout을 옵션을 지정할 수 있다.

Update API

Script는 업데이트 작업을 수행하는 데 사용되며, 버전 관리는 get 및 re-index하는 동안 업데이트가 발생하지 않았는지 확인하는 데 사용된다.

Command Example

POST schools/_update/4
{
   "script" : {
      "source": "ctx._source.name = params.sname",
      "lang": "painless",
      "params" : {
         "sname" : "City Wise School"
      }
   }
 }

정상 출력

{
  "_index" : "schools",
  "_type" : "_doc",
  "_id" : "4",
  "_version" : 3,
  "result" : "updated",
  "_shards" : {
  "total" : 2,
  "successful" : 1,
  "failed" : 0
  },
  "_seq_no" : 4,
  "_primary_term" : 2
}
profile
데이터 엔지니어링에 관심이 많은 홀로 삽질하는 느림보

0개의 댓글