【问题标题】:How to add a new field to ElasticSearch?如何向 ElasticSearch 添加新字段?
【发布时间】:2017-04-21 21:56:27
【问题描述】:

我正在尝试向 elasticsearch 添加一个名为“dateAdded”的新字段,该字段也将用于对数据进行排序。不知道如何使用 nodejs。

我在看这里?

https://www.elastic.co/guide/en/elasticsearch/reference/current/docs-update.html

我只看到了一个使用 curl 的示例,我该如何使用 nodejs 和这个模块 - https://github.com/elastic/elasticsearch-js

这是我最初索引它的方式:

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
                                }

                            });

【问题讨论】:

    标签: javascript node.js database elasticsearch


    【解决方案1】:

    这个问题的答案会根据您的 elasticsearch 集群的设置方式而有所不同。

    如果您有默认设置,那么您将启用动态映射。如果是这种情况,那么添加一个新字段就像索引带有该字段的文档一样简单,elasticsearch 将推断出什么映射是合适的,并将该字段添加到该类型的映射中。

    https://www.elastic.co/guide/en/elasticsearch/guide/current/dynamic-mapping.html

    如果您关闭了动态映射,那么您只需要对类型执行 PUT 映射命令

    PUT indexname/_mapping/mytype 
    {
      "properties": {
        "dateAdded": {
           "type": "date",
           "format": "dateOptionalTime"
        }
      }
    }
    

    看看这个:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

    因此,考虑到这一点,如果您设置了动态映射,您只需要使用更新 API,它就会为您制定新的映射。

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

    【讨论】:

    • 嘿,所以我包括了我最初是如何索引它的,所以我假设它是默认的,你知道我会如何用 npm 模块调用它吗?它仍然是 esclient.index 还是要进行不同的调用?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多