빅데이터/ELK
엘라스틱 기본 쿼리
STUFIT
2022. 6. 3. 20:19
반응형
1. index 생성 및 도큐먼트 생성
- 도큐먼트 생성 시, 해당 인덱스가 없을 경우에는 자동으로 인덱스를 생성하면서 도큐먼트를 인덱싱함.
PUT index2/_doc/1
{
"name" : "rhksdud23000",
"age" : 32,
"gender" : "male"
}
2. 인덱스 확인
GET index2
// 결과값
{
"index2" : {
"aliases" : { },
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"country" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"gender" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "index2",
"creation_date" : "1654095502930",
"number_of_replicas" : "1",
"uuid" : "YhNq8hXHRO-h4kWJwO8aAQ",
"version" : {
"created" : "8020299"
}
}
}
}
}
3. 도큐먼트 읽기
- 도큐먼트는 도큐먼트 아이디를 이용하여 특정 도큐먼트를 읽거나, 모든 토큐먼트를 읽을 수 있다.
// 특정 도큐먼트 읽기
GET index2/_doc/1
// 결과값
{
"_index" : "index2",
"_id" : "1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "rhksdud30000",
"country" : "korea",
"age" : 20,
"gender" : "female"
}
}
// 모든 도큐먼트 읽기
GET index2/_search
// 결과값
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "index2",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"name" : "rhksdud30000",
"country" : "korea",
"age" : 20,
"gender" : "female"
}
},
{
"_index" : "index2",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "park",
"age" : 22,
"gender" : "female"
}
},
{
"_index" : "index2",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "lee",
"age" : 42,
"gender" : "male"
}
}
]
}
}
4. 도큐먼트 수정
- 도큐먼트 수정은 PUT을 이용하여 덮어쓰기가 되어 업데이트 되는것처럼 보인다
- 혹은 update API 를 이용하여 특정 도큐먼트의 값을 업데이트 할 수 있다.
// 도큐먼트 수정(PUT)
PUT index2/_doc/1
{
"name" : "wow",
"country" : "USA",
"age":40,
"gender":"female"
}
// 결과값
{
"_index" : "index2",
"_id" : "1",
"_version" : 3,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 16,
"_primary_term" : 1
}
// 도큐먼트 수정(UPDATE API)
POST index2/_update/1
{
"doc": {
"name": "update_lky"
}
}
// 결과값
{
"_index" : "index2",
"_id" : "1",
"_version" : 4,
"_seq_no" : 17,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "update_lky",
"country" : "USA",
"age" : 40,
"gender" : "female"
}
}
5. 도큐먼트 삭제
- 특정 도큐먼트를 삭제하기 위해서는 인덱스명과 도큐먼트 아이디를 알고 있어야 삭제 가능하다.
// 도큐먼트 삭제
DELETE index2/_doc/1
// 결과값
{
"_index" : "index2",
"_id" : "1",
"_version" : 5,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 18,
"_primary_term" : 1
}
* 만약 인증이 걸려있는 상태면 curl 날릴 때, -u id:password를 붙여줘야함
ex) curl -X GET "http://localhost:9200/index2/_mapping" -u elastic:changeme
* 형식 오류가 뜨면 curl 날릴 때, -H'Content-type: application/json' 을 붙여줘야함
반응형