전체 그래프
Elasticsearch

REST API

data-engineeringelasticsearchapicrud

상위: Elasticsearch

요약

Elasticsearch는 RESTful API를 통해 데이터의 생성(Create), 조회(Read), 수정(Update), 삭제(Delete) 작업을 수행합니다.

REST API 특징

  • HTTP 기반 (GET, POST, PUT, DELETE)
  • 자원 중심 URL (/index/_doc/id)
  • JSON 데이터 포맷

Document CRUD

Create (Indexing)

문서를 생성하거나 덮어씁니다.

# Python Client 예시
doc = {
  "name": "Samsung Galaxy S24 Ultra",
  "price": 1199.99
}
response = es.index(index="products", id=1001, document=doc)

Read (Get)

ID로 문서를 조회합니다.

response = es.get(index="products", id=1001)

Update

문서의 일부 필드만 수정하거나 추가합니다.

update_body = {
  "doc": {
    "price": 1099
  }
}
response = es.update(index="products", id=1001, body=update_body)

Upsert

문서가 존재하면 Update, 없으면 Create를 수행합니다.

update_body = {
  "doc": { "price": 1099 },
  "doc_as_upsert": True
}
response = es.update(index="products", id=1001, body=update_body)

Delete

문서를 삭제합니다. 실제로는 삭제 플래그(tombstone)가 표시되며, 나중에 세그먼트 병합 시 물리적으로 삭제됩니다.

response = es.delete(index="products", id=1001)

내부 동작 원리

  • 불변성 (Immutability): 세그먼트는 수정되지 않습니다. Update는 기존 문서를 '삭제' 표시하고 새 문서를 '생성'하는 방식으로 동작합니다.
  • Flush: 메모리 버퍼에 있는 문서를 디스크의 세그먼트로 기록합니다.
  • Refresh: 버퍼의 내용을 검색 가능한 상태(새 세그먼트)로 만듭니다 (기본 1초).