Elastic
[Elasticsearch7.8.0] Script를 통한 특정 Field의 document에 데이터Append
신우섭
2020. 7. 31. 19:36
Elaticsearch를 이용하는 중
하나의 Field에 추가적으로 데이터를 넣어야하는 경우가 발생하였다.
서칭과 테스트 결과 update API와 Script를 이용하면 구현이 가능하였다.
예시
1. Index 생성
mehtod : PUT
http://localhost:9200/users
{
"settings": {
"index": {
"number_of_shards": 4,
"number_of_replicas": 1
}
},
"mappings": {
"properties": {
"name" : { "type" : "text" },
"user_log": {"type": "object"}
}
}
}
2. 필드에 데이터 추가
mehtod : POST
http://localhost:9200/users/_update/1
{
"script" : {
"source": "LinkedHashMap obj = new LinkedHashMap(); obj.put('files', '12334'); int i = 10; while (i > 0) {ctx._source.files.add(obj); i--}"
}
}
3. 결과
결과 잘 나오지만... 테스트를 한다고 100만개의 데이터를 넣었더니 출력 불가..
어쨌든 잘 나옵니다ㅎ