Skip to content

Instantly share code, notes, and snippets.

@pmuellr
Created December 14, 2022 18:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmuellr/85c815eb0b62fc9c0dccc4745e9561ab to your computer and use it in GitHub Desktop.
Save pmuellr/85c815eb0b62fc9c0dccc4745e9561ab to your computer and use it in GitHub Desktop.
Convert Elasticsearch "hits" JSON response to NDJSON format for easy importing back to Elasticsearch
#!/usr/bin/env node
const fs = require('fs')
const [ fileName ] = process.argv.slice(2)
if (fileName == null) {
console.error('input file with search response required')
process.exit(0)
}
const contents = fs.readFileSync(fileName, 'utf-8')
const json = JSON.parse(contents)
const hits = json.hits.hits
const output = hits.map(hit => JSON.stringify(hit._source)).join('\n')
console.log(output)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment