最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to add a new field to ElasticSearch? - Stack Overflow

programmeradmin3浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 9

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 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

发布评论

评论列表(0)

  1. 暂无评论