Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use "script fields" on nested type in ElasticSearch?

Tags:

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

like image 275
rongdong.bai Avatar asked Mar 28 '16 12:03

rongdong.bai


People also ask

What is nested type in ElasticSearch?

The nested type is a specialised version of the object data type that allows arrays of objects to be indexed in a way that they can be queried independently of each other.

What nested fields?

When a packed class contains an instance field that is a packed type, the data for that field is packed directly into the containing class. The field is known as a nested field .


1 Answers

This script should work:

"script_fields": {
    "custom_field": {
        "script": {
            "lang": "painless",
            "source": "params['_source']['comments'].size()"
        }
    }
}
like image 173
haitao Avatar answered Nov 08 '22 12:11

haitao