[Elastic][Elasticsearch] Runtime fields
Updated:
개요
- https://www.elastic.co/guide/en/elasticsearch/reference/current/runtime.html
- 쿼리 시 평가되는 필드
- 리인덱싱하지 않고 기존 문서에 필드 추가 가능
- 데이터 구조를 몰라도 데이터 작업 시작 가능
- 쿼리 시 인덱싱된 필드에서 반환된 값 재정의 가능
- 기본 스키마를 수정하지 않고 특정 용도에 대한 필드 정의 가능
예제
- 데이터
POST painless_test/_doc { "@timestamp": "2022-05-01T07:00:00.000Z", "cluster": "cluster-01", "resource": [ { "name": "node-01", "cpu": 1, "memory": 11, "disk": 111 }, { "name": "node-02", "cpu": 2, "memory": 22, "disk": 222 }, { "name": "node-03", "cpu": 3, "memory": 33, "disk": 333 } ] }
- 설정
PUT painless_test/_mapping { "runtime": { "cpu_sum": { "type": "double", "script": "double sum = 0; for (item in params._source.resource) {sum += item.cpu;} emit(sum);" } } }
- 요청
GET painless_test/_search { "query": { "match_all": {} }, "_source": [ "fields" ], "fields": [ "cpu_sum" ] }
- 응답
{ "took" : 0, "timed_out" : false, "_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 }, "hits" : { "total" : { "value" : 1, "relation" : "eq" }, "max_score" : 1.0, "hits" : [ { "_index" : "painless_test", "_type" : "_doc", "_id" : "xGa2aYABFxYHVJl737Jm", "_score" : 1.0, "_source" : { }, "fields" : { "cpu_sum" : [ 6.0 ] } } ] } }