Sample Data
$ wget https://gist.githubusercontent.com/devops-school/0a09b9e55efb45bcf397d501feb38965/raw/92179a491c6a717c70280619a8a6e3e64b2b0de3/customers_full.json
======================================================================================= | |
Elasticsearch Lab#2 – Bulk indexing document using mget & bulk api | |
======================================================================================= | |
Elasticsearch-mget-bulk | |
======================================================================================= | |
Multi-get | |
======================================================================================= | |
curl -XGET 'localhost:9200/products/_search?pretty=true&q=*:*' | |
curl -XGET 'localhost:9200/_mget?pretty' -H 'Content-Type: application/json' -d' | |
{ | |
"docs" : [ | |
{ | |
"_index" : "products", | |
"_type" : "mobiles", | |
"_id" : "nL_Fd3EBQb932Ywx993J" | |
}, | |
{ | |
"_index" : "products", | |
"_type" : "mobiles", | |
"_id" : "2" | |
} | |
] | |
} | |
' | |
curl -XGET 'localhost:9200/_mget?pretty' -H 'Content-Type: application/json' -d' | |
{ | |
"docs" : [ | |
{ | |
"_index" : "products", | |
"_type" : "mobiles", | |
"_id" : "1" | |
}, | |
{ | |
"_index" : "products", | |
"_type" : "mobiles", | |
"_id" : "2" | |
} | |
] | |
} | |
' | |
curl -XGET 'localhost:9200/products/_mget?pretty' -H 'Content-Type: application/json' -d' | |
{ | |
"docs" : [ | |
{ | |
"_type" : "mobiles", | |
"_id" : "1" | |
}, | |
{ | |
"_type" : "mobiles", | |
"_id" : "2" | |
} | |
] | |
} | |
' | |
curl -XGET 'localhost:9200/products/mobiles/_mget?pretty' -H 'Content-Type: application/json' -d' | |
{ | |
"docs" : [ | |
{ | |
"_id" : "1" | |
}, | |
{ | |
"_id" : "2" | |
} | |
] | |
} | |
' | |
======================================================================================= | |
Index multiple documents | |
======================================================================================= | |
curl -XPOST 'localhost:9200/_bulk?pretty' -H 'Content-Type: application/json' -d' | |
{ "index" : { "_index" : "products", "_type" : "mobiles", "_id" : "3" } } | |
{ "name": "Puma","size": 9,"color": "black" } | |
{ "index" : { "_index" : "products", "_type" : "mobiles", "_id" : "4" } } | |
{ "name": "New Balance","size": 8,"color": "black" } | |
' | |
curl -XPOST 'localhost:9200/products/_bulk?pretty' -H 'Content-Type: application/json' -d' | |
{ "index" : {"_type" : "shoes", "_id" : "3" } } | |
{ "name": "Puma","size": 9,"color": "black" } | |
{ "index" : {"_type" : "shoes", "_id" : "4" } } | |
{ "name": "New Balance","size": 8,"color": "black" } | |
' | |
curl -XPOST 'localhost:9200/products/shoes/_bulk?pretty' -H 'Content-Type: application/json' -d' | |
{ "index" : {"_id" : "3" } } | |
{ "name": "Puma","size": 9,"color": "black" } | |
{ "index" : {"_id" : "4" } } | |
{ "name": "New Balance","size": 8,"color": "black" } | |
' | |
======================================================================================= | |
Auto-generate ids | |
======================================================================================= | |
curl -XPOST 'localhost:9200/products/mobiles/_bulk?pretty' -H 'Content-Type: application/json' -d' | |
{ "index" : {} } | |
{ "name": "Puma","size": 9,"color": "black" } | |
{ "index" : {} } | |
{ "name": "New Balance","size": 8,"color": "black" } | |
' | |
======================================================================================= | |
Bulk operations in one go (paste these one operation at a time) | |
======================================================================================= | |
curl -XPOST 'localhost:9200/products/shoes/_bulk?pretty' -H 'Content-Type: application/json' -d' | |
{ "index" : {"_id" : "3" } } | |
{ "name": "Puma","size": 9,"color": "black" } | |
{ "index" : {"_id" : "4" } } | |
{ "name": "New Balance","size": 8,"color": "black" } | |
{ "delete" : {"_id" : "2" } } | |
{ "create" : {"_id" : "5" } } | |
{ "name": "Nike Power","size": 12,"color": "black" } | |
{ "update" : {"_id" : "1"} } | |
{ "doc" : {"color" : "orange"} } | |
' | |
======================================================================================= | |
Bulk indexing documents from a JSON file | |
======================================================================================= | |
How to upload BULK data for Query DSL? | |
============================================= | |
cd | |
sudo yum install wget -y | |
wget https://www.devopsschool.com/notes/elastic/elasticsearch/customers_full.json | |
$ curl -XPUT 'localhost:9200/customers?&pretty' | |
$ curl -H "Content-Type: application/x-ndjson" -XPOST 'localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"customers_full.json" | |
$ curl -H "Content-Type: application/x-ndjson" -XPOST 'localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"sam.json" | |
$ curl -H "Content-Type: application/x-ndjson" -XPOST 'localhost:9200/customers/personal/_bulk?pretty&refresh' --data-binary @"customers_full.json" | |
{ | |
"error" : { | |
"root_cause" : [ | |
{ | |
"type" : "illegal_argument_exception", | |
"reason" : "The bulk request must be terminated by a newline [\\n]" | |
} | |
], | |
"type" : "illegal_argument_exception", | |
"reason" : "The bulk request must be terminated by a newline [\\n]" | |
}, | |
"status" : 400 | |
} | |
FIX - Add a New line in customers_full.json or URL BODY | |
curl -XGET 'localhost:9200/_cat/indices?v&pretty' |
Reference
- Sample Data & Query – https://github.com/devopsschool-demo-labs-projects/elasticsearch
I’m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I am working at Cotocus. I blog tech insights at DevOps School, travel stories at Holiday Landmark, stock market tips at Stocks Mantra, health and fitness guidance at My Medic Plus, product reviews at I reviewed , and SEO strategies at Wizbrand.
Please find my social handles as below;
Rajesh Kumar Personal Website
Rajesh Kumar at YOUTUBE
Rajesh Kumar at INSTAGRAM
Rajesh Kumar at X
Rajesh Kumar at FACEBOOK
Rajesh Kumar at LINKEDIN
Rajesh Kumar at PINTEREST
Rajesh Kumar at QUORA
Rajesh Kumar at WIZBRAND