positionToIndex.js 1.37 KB
const axios = require("axios");
const log = console.log;

const esClient = require('./client');
const fs = require('fs');



async function indexData() {
    try {
      const aptRaw = await fs.readFileSync('./position.json');
      const aptInfo = JSON.parse(aptRaw);
      const apt_list = []
      for(const item of aptInfo.data){
          const tmp_data = {
              x : item.x,
              y : item.y,
              tracking_id : item.id,
              date : item.time.substring(0,19),
              seconds : item.s
          }
          apt_list.push(tmp_data)
      }
      const body = apt_list.flatMap(doc => [{ index: { _index: 'position', _type: 'position_info' } }, doc])
      const { body: bulkResponse } = await esClient.bulk({ refresh: true, body })

      if (bulkResponse.errors) {
          const erroredDocuments = []
          bulkResponse.items.forEach((action, i) => {
            const operation = Object.keys(action)[0]
            if (action[operation].error) {
              erroredDocuments.push({
                status: action[operation].status,
                error: action[operation].error,
                operation: body[i * 2],
                document: body[i * 2 + 1]
              })
            }
          })
          console.log(erroredDocuments)
          log('bulk error')
        } 
    } catch (error) {
    log('에러')
    }
  }

indexData();