Reputation: 4482
I am trying to add a new field to elasticsearch - called "dateAdded" which will be used to sort the data as well. Not sure how to go about it with nodejs.
I am looking here?
https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html
I only see an example for using curl, how can I do it using nodejs with this module - https://github.com/elastic/elasticsearch-js
This is how I originally indexed it:
esclient.index({
index: 'projects',
type: 'project',
id: projectIdStr,
body: {
"title": proj.title,
"company": proj.company,
"tags": proj.tagsArray,
"defaultPicId": proj.defaultPicId
}
},function(err,resp,status) {
if(err) { console.log(err);} else {
//stored
}
});
Upvotes: 4
Views: 41106
Reputation: 198
The answer to this will vary a little based on how your elasticsearch cluster is set up.
If you have default settings, then you will have dynamic mapping on. If this is the case then adding a new field is as simple as indexing a document with that field on it, elasticsearch will infer what mapping is appropriate and add that field to the mapping for that type.
https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html
If you have dynamic mapping off, then you will just need to do a PUT mapping command on the type
PUT indexname/_mapping/mytype
{
"properties": {
"dateAdded": {
"type": "date",
"format": "dateOptionalTime"
}
}
}
Take a look at this: https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html
So with that in mind, if you have dynamic mapping set up, you just need to use the update API and it will work out the new mapping for you.
Upvotes: 9