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?
.html
I only see an example for using curl, how can I do it using nodejs with this module -
This is how I originally indexed it:
esclient.index({
index: 'projects',
type: 'project',
id: projectIdStr,
body: {
"title": proj.title,
"pany": projpany,
"tags": proj.tagsArray,
"defaultPicId": proj.defaultPicId
}
},function(err,resp,status) {
if(err) { console.log(err);} else {
//stored
}
});
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./elastic/elasticsearch-js
This is how I originally indexed it:
esclient.index({
index: 'projects',
type: 'project',
id: projectIdStr,
body: {
"title": proj.title,
"pany": proj.pany,
"tags": proj.tagsArray,
"defaultPicId": proj.defaultPicId
}
},function(err,resp,status) {
if(err) { console.log(err);} else {
//stored
}
});
Share
Improve this question
edited Dec 8, 2016 at 6:19
Lion789
asked Dec 7, 2016 at 4:13
Lion789Lion789
4,48212 gold badges60 silver badges101 bronze badges
1 Answer
Reset to default 9The 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 mand 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.
https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update