【问题标题】:How to load (indexing) json file using elasticsearch and nodejs如何使用elasticsearch和nodejs加载(索引)json文件
【发布时间】:2021-10-24 10:01:58
【问题描述】:

我正在阅读the nodejs elasticsearch doc,示例中显示它们仅索引 3 个样本,但我想索引或加载包含 1000 个文档的 json 文件,您能帮帮我吗?

【问题讨论】:

    标签: node.js elasticsearch


    【解决方案1】:

    我通过以下方式解决了这个问题:

    //initialize the client
    const { Client } = require('@elastic/elasticsearch')
    const client = new Client({ node: 'http://localhost:9200' })
    
    // to read json files
    var fs = require('fs');
    / Start reading the json file
    fs.readFile('DocRes.json', {encoding: 'utf-8'}, function(err, data) {
        if (err) { throw err; }
      
        // Build up a giant bulk request for elasticsearch.
        bulk_request = data.split('\n').reduce(function(bulk_request, line) {
          var obj, ncar;
      
          try {
            obj = JSON.parse(line);
          } catch(e) {
            console.log('Done reading 1');
            return bulk_request;
          }
      
          // Rework the data slightly
          ncar = {
            id: obj.id,
            name: obj.name,
            summary: obj.summary,
            image: obj.image,
            approvetool: obj.approvetool,
            num: obj.num, 
            date: obj.date,
          };
      
          bulk_request.push({index: {_index: 'ncar_index', _type: 'ncar', _id: ncar.id}});
          bulk_request.push(ncar);
          return bulk_request;
        }, []);
      
        // A little voodoo to simulate synchronous insert
        var busy = false;
        var callback = function(err, resp) {
          if (err) { console.log(err); }
      
          busy = false;
        };
      
        // Recursively whittle away at bulk_request, 1000 at a time.
        var perhaps_insert = function(){
          if (!busy) {
            busy = true;
            client.bulk({
              body: bulk_request.slice(0, 1000)
            }, callback);
            bulk_request = bulk_request.slice(1000);
            console.log(bulk_request.length);
          }
      
          if (bulk_request.length > 0) {
            setTimeout(perhaps_insert, 100);
          } else {
            console.log('Inserted all records.');
          }
        };
      
        perhaps_insert();
      });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-07
      • 2017-10-14
      • 2012-12-12
      • 2018-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-01-09
      • 1970-01-01
      相关资源
      最近更新 更多