개발/Elasticsearch

Elasticsearch APIs

이리프리엘 2020. 5. 18. 20:10

Elasticsearch는 RESTful 방식의 API를 제공하며, 이를 통해 JSON 기반으로 통신함

기본적으로 http 통신을 위해 9200 port를 사용함

 

- Indices API : 인덱스 관리

- Document API : 문서 C/U/D

- Search API : 문서 Read

- Aggregation API : 문서 통계

 

Index 생성

Index 생성 시 mapping을 지정할 수 있는데, 이는 변경될 수 없으며 변경이 필요할 시 데이터를 삭제하고 재색인 하는 수밖에 없다.

PUT /character
{
	"settings": {
    	"number_of_shards": 3,
        "number_of_replicas": 2
    },
    "mappings": {
    	"_doc": {
        	"properties": {
            	"level": { "type" : "integer" }	// 현재 시점 ES버전에서는 Type을 제공하지 않음
                "name": { "type" : "text" }
                // ... 생략 ...
            }
        }
    }
}

// 결과
{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "character"
}

 

Index 삭제

DELETE /character

// 결과
{
  "acknowledged": false
}

 

Doc 생성

POST /character/_doc/1
{
  "level": 10,
  "name": "게임 캐릭터1"
}

// 결과
{
  "_index": "character",
  "_type": "_doc",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 2,
    "failed": 0
  }
}