5

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?".

2
  • 1
    Have a look at stackoverflow.com/a/27953707/4188368 Commented Mar 28, 2016 at 12:03
  • 1
    Thanks. "script": "_source.comments.values.size()" did work. Commented Mar 28, 2016 at 12:36

1 Answer 1

4

This script should work:

"script_fields": {
    "custom_field": {
        "script": {
            "lang": "painless",
            "source": "params['_source']['comments'].size()"
        }
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

is it possible to perform script not for aggregation, like .size(). but for inner field for each nested object? like params['_source']['comments']['likeCount'] * 2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.