简体   繁体   中英

How to add a new field to ElasticSearch?

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
                                }

                            });

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.

https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#api-update

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM