Skip to content

Instantly share code, notes, and snippets.

@wigsy
Created January 16, 2013 00:33
Show Gist options
  • Save wigsy/4543542 to your computer and use it in GitHub Desktop.
Save wigsy/4543542 to your computer and use it in GitHub Desktop.
Updating ES mappings
* Using the interactions/comment index as an example *
1) Current mappings that do not include collection_id:
curl http://localhost:9200/interactions/comment/_mapping?pretty=true
{
"comment" : {
"properties" : {
"body" : {
"type" : "string"
},
"created_on" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"id" : {
"type" : "long"
},
"media_id" : {
"type" : "long"
},
"media_title" : {
"type" : "string"
},
"media_type" : {
"type" : "string"
},
"reviewed" : {
"type" : "long"
},
"site_id" : {
"type" : "long"
},
"user_email" : {
"type" : "string"
},
"user_id" : {
"type" : "long"
},
"user_name" : {
"type" : "string"
}
}
}
2) PUT the following field mapping to an index.
Elasticsearch will merge the existing, and new indexes together.
In our case this works since we are adding a new field. It doesn't work so easily going the other way.
curl -XPUT http://localhost:9200/interactions/comment/_mapping?pretty=true --data @comment_mapping.json
{
"comment" : { "properties" : {
"collection_id" : {
"type" : "long"
}
}
}
}
3) Check that the new field is in place:
curl http://localhost:9200/interactions/comment/_mapping?pretty=true
{
"comment" : {
"properties" : {
"body" : {
"type" : "string"
},
"collection_id" : {
"type" : "long"
},
"created_on" : {
"type" : "date",
"format" : "dateOptionalTime"
},
"id" : {
"type" : "long"
},
"media_id" : {
"type" : "long"
},
"media_title" : {
"type" : "string"
},
"media_type" : {
"type" : "string"
},
"reviewed" : {
"type" : "long"
},
"site_id" : {
"type" : "long"
},
"user_email" : {
"type" : "string"
},
"user_id" : {
"type" : "long"
},
"user_name" : {
"type" : "string"
}
}
}
}
4) Confirm that an existing comment doesn't have any collection_id information:
curl http://localhost:9200/interactions/comment/36773?pretty=true
{
"_index" : "interactions",
"_type" : "comment",
"_id" : "36773",
"_version" : 1,
"exists" : true, "_source" : {"body": "<p>Thanks Christina, well done !</p>", "media_id": 486077, "user_id": 14260, "site_id": 3977, "reviewed": 1, "created_on": "2012-12-10T19:56:44", "media_title": "MediaCore Holiday Party 2012", "media_type": "video", "user_name": "Neil Schuler", "id": 36773, "user_email": "neil@mediacore.com"}
5) Use Elasticsearchs own update field scripting to set the new field values as required.
curl -XPOST http://localhost:9200/interactions/comment/36773/_update -d @update_collection_id.json
{
"script": "ctx._source.collection_id = new_id",
"params" : {
"new_id" : 123456 }
}
}
6) Confirm that the collection_id now exists for this comment
curl http://localhost:9200/interactions/comment/36773?pretty=true
{
"_index" : "interactions",
"_type" : "comment",
"_id" : "36773",
"_version" : 2,
"exists" : true, "_source" : {"body":"<p>Thanks Christina, well done !</p>","media_id":486077,"user_id":14260,"site_id":3977,"reviewed":1,"created_on":"2012-12-10T19:56:44","media_title":"MediaCore Holiday Party 2012","media_type":"video","user_name":"Neil Schuler","id":36773,"user_email":"neil@mediacore.com","collection_id":123456}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment