Import JSON into ElasticSearch


(Adityo Setyonugroho) #1

I would like to know how to import JSON data into ElasticSearch. I have used this command but it won't work:

curl -XPOST 'http://myipaddress:9200/demo-shop/items/' -H 'Content-Type: application/json' --data-binary @data.json --user username:password

This is the error:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"failed to parse"}],"type":"mapper_parsing_exception","reason":"failed to parse","caused_by":{"type":"not_x_content_exception","reason":"Compressor detection can only be called on some xcontent bytes or compressed xcontent bytes"}},"status":400}

Thank You

Best Regards


(Mark Walkom) #2

What's in the file?


(Adityo Setyonugroho) #3

This is a sample inside json:

[{
	"order": 452,
	"brand": ["Samsung"],
	"price": 2000000.0,
	"content": ["samsung battery pack animal edition mendukung upaya mempertahankan pelestarian spesies langka sama halnya seperti daya baterai perangkat mobile yang digunakan selalu kita pertahankan mengisi baterai perangkat mobile lebih nyaman mudah dan kian praktis dengan samsung battery pack animal edition karena telah dilengkapi kabel internal micro 5pin yang bisa langsung digunakan sebagai konektor ukurannya yang ramping membuat samsung battery pack animal edition sangat fleksibel untuk dibawa kemanamana saat bepergian dan optimal untuk pengisian baterai perangkat mobile lebih dari satu kali berkat kapasitas 11300 mah yang dimilikinya samsung battery pack animal edition memiliki sel baterai resmi samsung sdi yang tersertifikasi dengan proses uji ketat sehingga kualitas keamanannya terjamin superior download aplikasi charge the life untuk memperoleh fitur visual gerakan interaktif lihat ekspresi lucu spesies langka tersebut ketika layar perangkat disentuh sungguh menyenangkan mengisi baterai dengan samsung battery pack animal edition"],
	"name": ["Battery Pack Samsung Animal edition 11300 mAh"],
	"language": ["id"],
	"image": ["https://ecs7.tokopedia.net/img/cache/200-square/catalog/2015/11/3/3454752/3454752_3b8cf3bc-c708-473f-9114-4c24715f4c02.png"],
	"id": "834",
	"color": ["White"],
	"rating": 4.0,
	"category": ["212 | gadget | Gadget", "854 | powerbank | Power Bank"],
	"price_group": ["low"]
}, {
	"order": 445,
	"brand": ["Xiaomi"],
	"price": 900000.0,
	"content": ["xiaomi mi power bank yang satu ini dilengkapi dengan kapasitas super besar yaitu 16000 mah menggunakan selsel batre liion premium dari lg dan panasonic dengan kepadatan energi mencapai 725whl menambah ekstra kapasitas daya sebesar 16000 mah untuk smartphone tablet atau kamera digital kamu mampu mengecas redmi note sampai penuh 35 kali mi power bank mengatur output daya secara otomatis tergantung gadget yang tersambung mengecas smartphone dan tablet dari mi apple samsung htc google dan blackberry juga bermacam kamera digital dan gadget game portable dengan mudah dapat mengecas dua gadget sekaligus dengan output maksimum 51v36a daya output lebih besar dari charger dualport biasa performa pengecasan ganda yang belum pernah dialami sebelumnya mi powerbank juga menggunakan usb smartcontrol dan chipset manajemen daya dari texas instruments yang memberikan perlindungan rangkaian chip sembilan lapis sekaligus meningkatkan efisiensi pengecasan"],
	"name": ["Xiaomi Mi Power Bank 16000 mAh"],
	"language": ["id"],
	"image": ["https://ecs7.tokopedia.net/img/cache/200-square/catalog/2015/11/3/3454752/3454752_cdc35905-8672-4ab5-9230-b342615d64aa.png"],
	"id": "844",
	"color": ["-"],
	"rating": 3.3,
	"category": ["212 | gadget | Gadget", "854 | powerbank | Power Bank"],
	"price_group": ["low"]
}, . . . . .

(Mark Walkom) #4

That's not valid json due to the beginning [. I guess there is probably an ending one too that will also be invalid.


(Christian Dahlqvist) #5

Although that is valid JSON, passing an array of objects does now work with Elasticsearch, as the root element must be a dictionary. If you are looking to index the objects within this array as separate documents, you would need to transform this into the bulk format.


(Mark Walkom) #6

True, thanks for clarifying :slight_smile:


(Adityo Setyonugroho) #7

Hi,

Sorry for my late response.

Thank you for your explanation. So, I need to assign a key to the array of object right ? e.g. {"items":[{}]}

From your point of view, is it better in one document or separate documents with this kind of data ?

Thank You


(Christian Dahlqvist) #8

I do not know the data or how you want to query it, but it looks to me like the objects are separate entities, so I suspect separate documents might be appropriate.


(Adityo Setyonugroho) #9

Sorry for my late reply.

I have successfully import the JSON, thanks


(system) #10

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.