Updated:

less than 1 minute read

개요


예제

  • 데이터
     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
         }
       ]
     }
    
  • 요청
     GET painless_test/_search
     {
       "query": {
         "match_all": {}
       },
       "_source": [
         "fields"
       ],
       "fields": [
         "cpu_sum",
         "timestamp"
       ],
       "runtime_mappings": {
         "cpu_sum": {
           "type": "double",
           "script": "double sum = 0; for (item in params._source.resource) {sum += item.cpu;} emit(sum);"
         },
         "timestamp": {
           "type": "keyword",
           "script": "emit(doc['@timestamp'].value.toString());"
         }
       }
     }
    
  • 응답
     {
       "took" : 1,
       "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
               ],
               "timestamp" : [
                 "2022-05-01T07:00:00.000Z"
               ]
             }
           }
         ]
       }
     }