본 내용은 윈도우에 설치된 Elasticsearch 7.10.2 버전으로 기준으로 작성되었습니다.
1. Index 생성하기
PUT http://localhost:9200/test_index
Header:
Content-Type: application/json
Body:
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"field1": { "type": "text" },
"field2": { "type": "text" },
"field3": { "type": "text" },
"field4": { "type": "text" }
}
}
}
응답:
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "test_index"
}
2. Index 생성 확인하기
GET http://localhost:9200/_cat/indices
응답:
green open test_index vOk2Qo-YTJSbIGtYSAVHkg 1 0 0 0 208b 208b
3. Index Field 정보 확인하기
GET http://localhost:9200/test_index/_mapping
응답:
{
"test_index": {
"mappings": {
"properties": {
"field1": {
"type": "text"
},
"field2": {
"type": "text"
},
"field3": {
"type": "text"
},
"field4": {
"type": "text"
}
}
}
}
}
4. Index에 Document 추가하기
다음 API를 사용하여 Index에 Document를 추가할 수 있습니다. ID 값을 지정하지 않고 추가하는 방법과 ID 값을 지정하여 추가하는 방법이 있습니다.
- ID 값을 지정하기 않고 추가하기
POST http://localhost:9200/test_index/_doc
Header:
Content-Type: application/json
Body:
{
"field1": "Elasticsearch Basics Test.",
"field2": "Elasticsearch Basics Test.",
"field3": "Elasticsearch Basics Test.",
"field4": "Elasticsearch Basics Test."
}
응답:
{
"_index": "test_index",
"_type": "_doc",
"_id": "JvwH9YYBZI6tAZLb_kao",
"_version": 1,
"result": "created",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
- ID 값을 지정하고 추가하기
PUT http://localhost:9200/test_index/_doc/2
Header, Body는 "ID 값을 지정하기 않고 추가하기" 예시와 동일하게 추가
차이점: URL 뒤에 id 값 넣어서 요청하기
5. 데이터 조회
GET http://localhost:9200/test_index/_search
응답
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "JvwH9YYBZI6tAZLb_kao",
"_score" : 1.0,
"_source" : {
"field1" : "Elasticsearch Basics Test.",
"field2" : "Elasticsearch Basics Test.",
"field3" : "Elasticsearch Basics Test.",
"field4" : "Elasticsearch Basics Test."
}
},
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "2",
"_score" : 1.0,
"_source" : {
"field1" : "Elasticsearch Basics Test.",
"field2" : "Elasticsearch Basics Test.",
"field3" : "Elasticsearch Basics Test.",
"field4" : "Elasticsearch Basics Test."
}
}
]
}
}
고유 값인 _id 값을 보면, 첫번째 넣은 데이터는 임의로 생성된 "JvwH9YYBZI6tAZLb_kao" 값을 가지고 있고,
두번째 넣은 데이터의 _id 값은 지정한 그대로 "2" 가 들어가 있는 것을 확인할 수 있다.
6. 데이터 업데이트
POST http://localhost:9200/test_index/_doc/2/_update
Header:
Content-Type: application/json
Body:
{
"doc": {
"field1": "Elasticsearch Basics Test. (Updated)",
"field2": "Elasticsearch Basics Test.",
"field3": "Elasticsearch Basics Test.",
"field4": "Elasticsearch Basics Test."
}
}
응답:
{
"_index": "test_index",
"_type": "_doc",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 2
}
7. 데이터 삭제
DELETE http://localhost:9200/test_index/_doc/1
응답:
{
"_index": "test_index",
"_type": "_doc",
"_id": "2",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 1,
"successful": 1,
"failed": 0
},
"_seq_no": 4,
"_primary_term": 2
}
Elasticsearch의 기본적인 사용 방법에 대해 알아보았습니다. Elasticsearch는 다양한 기능을 제공하며, 이를 활용하여 다양한 검색 요건을 충족시킬 수 있습니다.
반응형
'BigData > ElasticSearch' 카테고리의 다른 글
Elasticsearch 노드의 종류와 역할 (feat 동작 흐름) (0) | 2023.03.22 |
---|---|
Elasticsearch에서 사용되는 주요 용어 10가지 (0) | 2023.03.21 |
Keyword Field를 활용하는 다양한 쿼리 예제 (0) | 2023.03.20 |
Elasticsearch 주요 field data types (V7.10 기준) (0) | 2023.03.17 |
Elasticsearch에서 동적 매핑과 필드 정의하기 (0) | 2023.03.13 |