Source: https://github.com/codingexplained/complete-guide-to-elasticsearch
Get cluster health verbose
GET /_cluster/health
Get the nodes verbose
GET /_cat/nodes?v
Get all indices verbose
GET /_cat/indices?v
Get all the shards verbose
GET /_cat/shards?v
Creating index
PUT /products
{
"settings": {
"number_of_shards": 2,
"number_of_replicas": 2
}
}
Creating products with random id
POST /products/_doc
{
"name": "Coffee Maker",
"price": 64,
"in_stock": 10
}
Create products with specified id. THIS WILL REPLACE OLD DOCUMENT FOR ID 100!!
PUT /products/_doc/100
{
"name": "Toaster",
"price": 49,
"in_stock": 4
}
Get the products of id
GET /products/_doc/100
Updating the documents, keep in mind the _doc/id/_update is for elastic version 6.x.x, version 7.x.x is _update/100 When updating, if the key is not found, it will add automatically //If key exists, use the new value
POST /products/_update/100
{
"doc": {
"in_stock": 3,
"tags": ["electronics"]
}
}
Scripted updates reduce stock value
POST /products/_update/100
{
"script": {
"source": "ctx._source.in_stock--"
}
}
Scripted updates specifying value
POST /products/_update/100
{
"script": {
"source": "ctx._source.in_stock=10"
}
}
Scripted updates with params
POST /products/_update/100
{
"script": {
"source": "ctx._source.in_stock -= params.quantity",
"params": {
"quantity": 4
}
}
}
Upsert, to either run script or create docs if nothing found
POST /products/_update/101
{
"script": {
"source": "ctx._source.in_stock++"
},
"upsert": {
"name": "Blender",
"price": 399,
"in_stock": 5
}
}
Delete a product / document.
DELETE /products/_doc/100
Update documents only if _seq_no and _primary_term is correct, for Optimistic Concurrency Control. Else ERROR 409 (Conflict)
POST /products/_update/100?if_primary_term=2&if_seq_no=17
{
"doc": {
"in_stock": 123
}
}
Update query, minus in_stock for all documents
POST /products/_update_by_query
{
"script": {
"source": "ctx._source.in_stock --"
},
"query": {
"match_all": {}
}
}
Delete by query
POST /products/_delete_by_query
{
"query": {
"match_all": {}
}
}
BULK API Create, using x-ndjson
POST /_bulk
{ "index": { "_index": "products", "_id": 200 } }
{ "name": "Espresso Machine", "price": 199, "in_stock": 5 }
{ "create": { "_index": "products", "_id": 201 } }
{ "name": "Milk", "price": 149, "in_stock": 14 }
BULK API Update/Delete
POST /_bulk
{ "update": { "_index": "products", "_id": 201 } }
{ "doc": {"price": 129 }}
{ "delete": { "_index": "products", "_id": 200 } }
POST /products/_bulk
{ "update": { "_id": 201 } }
{ "doc": {"price": 129 }}
{ "delete": { "_id": 200 } }
cURL bulk insert with file example
curl -H "Content-Type: application/x-ndjson" -XPOST http://localhost:9200/products/_bulk --data-binary "@products-bulk.json"