Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Is there any way to import a JSON file (contains 100 documents) in elasticsearch server? I want to import a big json file into es-server..

like image 952
shailendra pathak Avatar asked Dec 17 '13 23:12

shailendra pathak


People also ask

Can JSON files have imports?

json file is set to module . Use an import assertion to import the JSON file. For example, import myJson from './example. json' assert {type: 'json'} .

Does Elasticsearch support JSON?

Elasticsearch only supports JSON. If you want to send something else you need to transform it. You can use logstash or whatever other system (even your code).


1 Answers

As dadoonet already mentioned, the bulk API is probably the way to go. To transform your file for the bulk protocol, you can use jq.

Assuming the file contains just the documents itself:

$ echo '{"foo":"bar"}{"baz":"qux"}' |  jq -c ' { index: { _index: "myindex", _type: "mytype" } }, . '  {"index":{"_index":"myindex","_type":"mytype"}} {"foo":"bar"} {"index":{"_index":"myindex","_type":"mytype"}} {"baz":"qux"} 

And if the file contains the documents in a top level list they have to be unwrapped first:

$ echo '[{"foo":"bar"},{"baz":"qux"}]' |  jq -c ' .[] | { index: { _index: "myindex", _type: "mytype" } }, . '  {"index":{"_index":"myindex","_type":"mytype"}} {"foo":"bar"} {"index":{"_index":"myindex","_type":"mytype"}} {"baz":"qux"} 

jq's -c flag makes sure that each document is on a line by itself.

If you want to pipe straight to curl, you'll want to use --data-binary @-, and not just -d, otherwise curl will strip the newlines again.

like image 129
Peter Avatar answered Sep 19 '22 17:09

Peter