약간 오버라이트 같은 데이터 수정을 해볼 것.
앞에서 delete로 인덱스를 날렸으니 조회하면 404가 나올거다., 일단 데이터 한번 넣어서 확인 한번 해보자.
~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"
{
"error" : {
"root_cause" : [
{
"type" : "index_not_found_exception",
"reason" : "no such index",
"resource.type" : "index_expression",
"resource.id" : "customer",
"index_uuid" : "_na_",
"index" : "customer"
}
],
"type" : "index_not_found_exception",
"reason" : "no such index",
"resource.type" : "index_expression",
"resource.id" : "customer",
"index_uuid" : "_na_",
"index" : "customer"
},
"status" : 404
}
이렇게 404...
~]$ curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
"name": "John Doe"
}
넣어주고.
확인해보자.
~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "John Doe"
}
}
음.... name 를 잘 보자. 수정은... 다음 과 같이.
~]$ curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
> {
> "name": "Joooooooooooooooon Duck"
> }
> '
res
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 1,
"_primary_term" : 1
}
조회 하면.
~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"name" : "Joooooooooooooooon Duck"
}
}
존덕....으로 덮어써진 것 같다. 다른점은 version 2 가 된점.
인덱스에 새로운 아이디(2번) 추가해보자.
~]$ curl -X PUT "localhost:9200/customer/_doc/2?pretty" -H 'Content-Type: application/json' -d'
> {
> "name": "Jane Doe"
> }
> '
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
얼추 드간거 같으니까. 1번 이랑 2번 한번 조회해보자.
~]$ curl -X GET "localhost:9200/customer/_doc/1?pretty"
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"found" : true,
"_source" : {
"name" : "Joooooooooooooooon Duck"
}
}
내계정명@내서버명 ~]$ curl -X GET "localhost:9200/customer/_doc/2?pretty"
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "2",
"_version" : 1,
"found" : true,
"_source" : {
"name" : "Jane Doe"
}
}
1,2 볼 수 있음.
1,2 지정해서 만들었는데, 미지정하면 알아서 autoincrement 하는 .... 것 같진 않고 지정하지 않으면 Elasticsearch에서 임의 ID를 생성하여 문서 색인화에 사용합니다. 라고 함.
그래서 미명시 put 도 해보려고...
앞에서 메모 한 <REST Verb> /<Index>/<Type>/<ID> 법칙엔 조금 벗어남.
~]$ curl -X PUT "localhost:9200/customer/_doc/pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Dodsfsfsdfsdfsdfe"
}
'
{"_index":"customer","_type":"_doc","_id":"pretty","_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
json pretty 가 사라졌다... 아무튼 들어간거 같음.
조회해보면
~]$ curl -X GET "localhost:9200/customer/_doc/pretty"
{"_index":"customer","_type":"_doc","_id":"pretty","_version":1,"found":true,"_source":
{
"name": "Jane Dodsfsfsdfsdfsdfe"
}
이런식으로 들어 감.
근데 이건 잘못사용한 예의 하나일 듯. (그래서 json이 이쁘게 안나온건가?)
put의 경우 id를 명시해 주는 경우에만 사용해야하는데, 지금 res를 잘 보면 id가 pretty 가 되었음.
가이드에 있는대로 써보면.
미 명시의 경우 POST를 사용한다. (id자동생성)
~]$ curl -X POST "localhost:9200/customer/_doc?pretty" -H 'Content-Type: application/json' -d'
{
"name": "Jane Doe12323456784567890"
}
'
라고 날리면, res로
{
"_index" : "customer",
"_type" : "_doc",
"_id" : "YrQSWmQBU_p42XDzzCOy",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
id가 자동 생성되었다.
아하. 그렇구나... id 지정 put , 자동지정 post..