Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add default values while adding a new field in existing mapping in elasticsearch

This is my existing mapping in elastic search for one of the child document

sessions" : {
    "_routing" : {
    "required" : true
    },
    "properties" : {
       "operatingSystem" : {
       "index" : "not_analyzed",
       "type" : "string"
    },
    "eventDate" : {
       "format" : "dateOptionalTime",
       "type" : "date"
    },
    "durations" : {
       "type" : "integer"
    },
    "manufacturer" : {
       "index" : "not_analyzed",
       "type" : "string"
    },
    "deviceModel" : {
       "index" : "not_analyzed",
       "type" : "string"
    },
    "applicationId" : {
       "type" : "integer"
    },
    "deviceId" : {
       "type" : "string"
    }
    },
    "_parent" : {
       "type" : "userinfo"
    }
}

in above mapping "durations" field is an integer array. I need to update the existing mapping by adding a new field called "durationCount" whose default value should be the size of durations array.

PUT sessions/_mapping
{
    "properties" : {
    "sessionCount" : {
      "type" :    "integer"
    }
  }
}

using above mapping I am able to update the existing mapping but I am not able to figure out how to assign a value ( which would vary for each session document like it should be durations array size ) while updating the mapping. any ideas ?

like image 316
Dead man Avatar asked Oct 20 '22 19:10

Dead man


1 Answers

Well 2 recommendations here -

  1. Instead of adding default value , you can adjust it in the query using missing filter. Lets say , you want to search based on a match query - Instead of just match query , use a bool query with should clause having the match and missing filter. inside filtered query. This way , those documents which did not have the field is also accounted.
  2. If you absolutely need the value in that field for existing documents , you need to reindex the whole set of documents. Or , use the out of box plugin , update by query -
like image 194
Vineeth Mohan Avatar answered Oct 22 '22 08:10

Vineeth Mohan