CentAu
CentAu

Reputation: 11160

Elasticsearch access the size of a certain field in the documents

I know that with field _size we can tell Elasticsearch to store the size of the document. This is the mapping that I defined for my index:

'{
   "mappings":{
      "_default_":{
         "_size":{
            "enabled":true,
            "store":true
         }
      }
   }
}'

The _size field gives the whole document's size where as I want only a certain field's size.

For example suppose we have two fields in the mapping. message and url. i.e.

curl -XPUT localhost:9200/indexname/tweet/00001 -d '{"message": "some message", "url" : "http://someurl.url"}'

Is it possible to get the length (or store size) of only the messages in the above mapping?

Upvotes: 2

Views: 5277

Answers (1)

Joe Enzminger
Joe Enzminger

Reputation: 11190

You can do this using script fields

{
    {
    "query" : {
        ...
    },
    "script_fields" : {
        "message_length" : {
            "script" : "doc['message'].value.size()"
        }
    }
}

Upvotes: 3

Related Questions