简体   繁体   中英

How to use “script fields” on nested type in ElasticSearch?

I want to do something like this, use script_fields to show the length of comments.

{
   "script_fields" : {
      "comments" : {
         "script" : "doc['comments'].values.size()"
     }
   }
}

but comments is a nested type. which looks like

{
   "comments": [
         {
             "k": "2016-01-06T00:00:03",
             "v": "v1 "
         },
         {
             "k": "2016-01-06T00:00:04",
             "v": "v2"
         }
   ]
}

so I want to know "how to use "script fields" on nested type in ElasticSearch?".

This script should work:

"script_fields": {
    "custom_field": {
        "script": {
            "lang": "painless",
            "source": "params['_source']['comments'].size()"
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM