简体   繁体   English

如何向ElasticSearch添加新字段?

[英]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. 我正在尝试向elasticsearch添加一个新字段-称为“ dateAdded”,该字段也将用于对数据进行排序。 Not sure how to go about it with nodejs. 不知道如何使用nodejs。

I am looking here? 我在看这里吗?

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html https://www.elastic.co/guide/zh-CN/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 我只看到一个使用curl的示例,如何使用带有该模块的nodejs- 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. 答案将根据您的Elasticsearch集群设置方式而有所不同。

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. 如果是这种情况,那么添加新字段就像索引带有该字段的文档一样简单,elasticsearch将推断出适合的映射并将该字段添加到该类型的映射中。

https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html https://www.elastic.co/guide/zh-CN/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映射命令

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 看看这个: 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. 因此,请记住,如果您设置了动态映射,则只需使用更新API,它将为您解决新的映射问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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