importJSONDoc()

in common-utils/network/test-fixture-handler.js [54:90]


  importJSONDoc(filename, bulkMax = 1600) {
    const parseDocument = ({ value: { id, index, source } }) => {
      const actionData = { index: { _id: id, _index: index } }
      const oneLineAction = JSON.stringify(actionData).replace('\n', '')
      const oneLineSource = JSON.stringify(source).replace('\n', '')
      const bulkOperation = `${oneLineAction}\n${oneLineSource}`
      return bulkOperation
    }
  
    const sendBulkAPIRequest = (bodyArray, openSearchUrl) => {
      this.testRunner.request({ headers: { 'Content-Type': 'application/json' }, method: 'POST', url: `${openSearchUrl}/_bulk`, body: `${bodyArray.join('\n')}\n`, timeout: 30000 }).then((response) => {
  
      })
    }

    this.testRunner.readFile(filename, 'utf8').then((str) => {
      let readJSONCount = 0
      const bulkLines = [[]]
      str.split('\n\n').forEach((element) => {
        bulkLines[0].push(parseDocument(JSON.parse(element)))
  
        readJSONCount++
        if (readJSONCount % bulkMax === 0) {
          sendBulkAPIRequest(bulkLines.pop(), this.openSearchUrl)
          bulkLines.push([])
        }
      })
  
      if (bulkLines.length > 0) {
        sendBulkAPIRequest(bulkLines.pop(), this.openSearchUrl)
      }
  
      this.testRunner.request({ method: 'POST', url: `${this.openSearchUrl}/_all/_refresh` }).then((response) => {
  
      })
    })
  }