【问题标题】:Elasticsearch: how can i create mapping before upload json data into ElasticsearchElasticsearch:如何在将 json 数据上传到 Elasticsearch 之前创建映射
【发布时间】:2015-12-03 18:41:28
【问题描述】:

我正在尝试在将 json 数据上传到 elasticsearch 之前创建映射。 在sails.js中上传json数据之前不知道怎么实现映射

这是我的批量上传 sn-p

     var body = [];
      //row is json data
        rows.forEach(function(row, id) {
             body.push({ index:  { _index: 'testindex', _type: 'testtype', _id: (id+1) } });
             body.push(row);
        })  
    client.bulk({
                    body: body
                }, function (err, resp) {
                        if (err) 
                        {
                            console.log(err);
                            return;
                       }
                      else 
                      { 
                            console.log("All Is Well");
                      }
      });

我想在数据上传之前创建映射。有人知道如何在sails中创建映射。

我的 Json 对象

 [ { Name: 'paranthn', Age: '43', Address: 'trichy' },
      { Name: 'Arthick', Age: '23', Address: 'trichy' },
      { Name: 'vel', Age: '24', Address: 'trichy' } ]

【问题讨论】:

    标签: json node.js elasticsearch sails.js


    【解决方案1】:

    在拨打client.bulk() 之前,您首先需要像这样拨打另一个client.indices.putMapping() 电话,以便为您即将通过bulk 电话发送的数据保存正确的映射:

    client.indices.putMapping({
       "index": "testindex",
       "type": "testtype",
       "body": {
          "testtype": {
              "properties": {
                  "your_int_field": {
                      "type": "integer"
                  },
                  "your_string_field": {
                      "type": "string"
                  },
                  "your_double_field": {
                      "type": "double"
                  },
                  // your other fields
              }
          }
       }
    }, function (err, response) {
       // from this point on, if you don't get any error, you may call bulk.
    });
    

    请记住,所有这些调用都是异步的,因此请注意仅在 putMapping 成功返回后才调用 bulk

    【讨论】:

    • 嗨,它对我来说很好用。但是我的 json 对象将所有内容都作为字符串返回,但是在我的属性中,我需要将属性设置为整数,我该如何实现。
    • 很高兴它有帮助!我已经更新了答案以包含一些您可以根据自己的喜好修改的示例字段。映射的详细信息here
    • JSON 中的字段数可能是动态的。那我该如何做映射呢?我已经更新了我的问题中的示例 json 数据,请参考。
    【解决方案2】:

    听起来你需要PutMapping

    【讨论】:

    • 但是在批量上传功能之前我需要在sails.js中进行映射。这可能吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    • 1970-01-01
    • 2019-02-02
    • 1970-01-01
    • 2016-07-05
    • 2014-12-25
    相关资源
    最近更新 更多