elastic search cheat sheet
Elastic Search Cheat Sheet with Kibana REST & curl commands¶
First Set ELASTICSEARCH URL
Elastic Search Administration¶
Get the health of the cluster¶
Get cluster stats¶
Get the cluster state¶
Cluster Information¶
Get Size of all indices¶
Get Size of Elastic Search data on each node vs node size¶
Get Node information¶
Get Thread Pool¶
Get total number of shards¶
Get total number of indices¶
Get List of Pending Tasks¶
Get List of All mappings¶
Get all plugins¶
Total Free Space in the CLuster¶
Format Elastic Search Output; Examples¶
Elastic Search DataBase Operations¶
Set Index Name
Create an index (default)¶
Create an index with mappings and settings¶
PUT /index-name
{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "integer"
}
}
}
}
curl -X PUT "$ELASTICSEARCH_URL/$INDEX_NAME" -H "Content-Type: application/json" -d '{
"settings": {
"number_of_shards": 1,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"price": {
"type": "integer"
}
}
}
}'
Add a document¶
curl -X POST "$ELASTICSEARCH_URL/$INDEX_NAME/_doc" -H "Content-Type: application/json" -d '{
"name": "Shivam",
"age": 24
}'
Get index mappings and settings¶
Empty an index¶
Delete an index¶
List All Indices (Most useful), Sort by descending order of size, show header and important columns only¶
GET /_cat/indices?v&s=store.size:desc&h=index,pri,rep,docs.count,docs.deleted,store.size,pri.store.size
curl -s -XGET "$ELASTICSEARCH_URL/_cat/indices?v&s=store.size:desc&h=index,pri,rep,docs.count,docs.deleted,store.size,pri.store.size"
List All Indices with metadata¶
Get All Indices list only¶
Get All Indices sorted in descending order of size¶
Get List of All aliases , Sort by index name¶
Get List of All aliases¶
Get all data inside the index¶
curl -s -XGET "$ELASTICSEARCH_URL/$INDEX_NAME/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'
Update an entire doc by it ID¶
curl -X PUT "$ELASTICSEARCH_URL/$INDEX_NAME/_doc/$DOC_ID" -H "Content-Type: application/json" -d '{
"name": "Anand",
"age": 25
}'
Update a specific field document in an index by its id¶
DOC_ID="<Get document id>"
curl -X POST "$ELASTICSEARCH_URL/$INDEX_NAME/_update/$DOC_ID" -H "Content-Type: application/json" -d '{
"doc": {
"age": 99
}
}'
Update all document in an index by script¶
POST /<index-name>/_update_by_query
{
"script": {
"source": "ctx._source.age += params.count",
"lang": "painless",
"params": {
"count": 10
}
}
}
curl -X POST "$ELASTICSEARCH_URL/$INDEX_NAME/_update_by_query" -H 'Content-Type: application/json' -d'
{
"script" : {
"source": "ctx._source.age += params.count",
"lang": "painless",
"params" : {
"count" : 10
}
}
}'
Bulk Operation using bulk API¶
POST /<index-name>/_update_by_query
{
"script": {
"source": "ctx._source.age += params.count",
"lang": "painless",
"params": {
"count": 10
}
}
}
curl -XPOST "$ELASTICSEARCH_URL/$INDEX_NAME/_bulk?pretty" -H "Content-Type: application/json" -d '
{"index":{"_id":"1"}
{"name": "Ramu","age": 45}
{"index":{"_id":"2"}
{"name": "Lemon","age": 1}
{"delete":{"_id":"1"}}
{"update":{"_id":"2"}}
{"doc":{"name":"Orange"}}
'
Get document count in an index¶
Make an index read-only¶
Block write to the source index / make the index read only
curl -X PUT "$ELASTICSEARCH_URL/$INDEX_NAME/_settings?pretty" -H 'Content-Type: application/json' -d '
{
"index.blocks.write": true
}'
Undo block write / Undo read-only mode
curl -X PUT "$ELASTICSEARCH_URL/$SOURCE_INDEX/_settings" -H 'Content-Type: application/json' -d '
{
"index.blocks.write": false
}'
Backup an index¶
Mandatory step to block write to the source index / make the index read only
curl -X PUT "$ELASTICSEARCH_URL/$SOURCE_INDEX/_settings?pretty" -H 'Content-Type: application/json' -d '
{
"index.blocks.write": true
}'
curl -s -XGET "$ELASTICSEARCH_URL/$DESTINATION_INDEX/_search?pretty" -H 'Content-Type: application/json' -d'
{
"query": {
"match_all": {}
}
}'
curl -X PUT "$ELASTICSEARCH_URL/$SOURCE_INDEX/_settings" -H 'Content-Type: application/json' -d '
{
"index.blocks.write": false
}'
Open and close indexes to save memory and CPU¶
close: A closed index is essentially frozen. Search and write operations are blocked.
open: An open index is available for search and write operations. This is the typical state for an index you're actively using for data storage and retrieval.