× {{alert.msg}} Never ask again
Get notified about new tutorials RECEIVE NEW TUTORIALS

is there any way to import a json file(contains 100 documents) in elasticsearch server.?

Deryck Henson
Feb 05, 2015
<p>I'm sure someone wants this so I'll make it easy to find.</p> <p>FYI - This is using <strong>Node.js</strong> (essentially as a batch script) on the same server as the brand new ES instance. Ran it on 2 files with 4000 items each and it only took about 12 seconds on my shared virtual server. YMMV</p> <pre><code>var elasticsearch = require('elasticsearch'), fs = require('fs'), pubs = JSON.parse(fs.readFileSync(__dirname + '/pubs.json')), // name of my first file to parse forms = JSON.parse(fs.readFileSync(__dirname + '/forms.json')); // and the second set var client = new elasticsearch.Client({ // default is fine for me, change as you see fit host: 'localhost:9200', log: 'trace' }); for (var i = 0; i &lt; pubs.length; i++ ) { client.create({ index: "epubs", // name your index type: "pub", // describe the data thats getting created id: i, // increment ID every iteration - I already sorted mine but not a requirement body: pubs[i] // *** THIS ASSUMES YOUR DATA FILE IS FORMATTED LIKE SO: [{prop: val, prop2: val2}, {prop:...}, {prop:...}] - I converted mine from a CSV so pubs[i] is the current object {prop:..., prop2:...} }, function(error, response) { if (error) { console.error(error); return; } else { console.log(response); // I don't recommend this but I like having my console flooded with stuff. It looks cool. Like I'm compiling a kernel really fast. } }); } for (var a = 0; a &lt; forms.length; a++ ) { // Same stuff here, just slight changes in type and variables client.create({ index: "epubs", type: "form", id: a, body: forms[a] }, function(error, response) { if (error) { console.error(error); return; } else { console.log(response); } }); } </code></pre> <p>Hope I can help more than just myself with this. Not rocket science but may save someone 10 minutes.</p> <p><em>Cheers</em></p> <p>This tip was originally posted on <a href="http://stackoverflow.com/questions/20646836/is%20there%20any%20way%20to%20import%20a%20json%20file(contains%20100%20documents)%20in%20elasticsearch%20server.?/25218368">Stack Overflow</a>.</p>
comments powered by Disqus