Elasticsearch Cheat Sheet

Here’s a cheat sheet for working with Elasticsearch, a popular distributed search and analytics engine:

Basic Commands

Start Elasticsearch:

bin/elasticsearch

Check Cluster Health:

curl -X GET "localhost:9200/_cat/health?v"

Check Nodes in Cluster:

curl -X GET "localhost:9200/_cat/nodes?v"

Index Operations

Create an Index:

curl -X PUT "localhost:9200/my_index"

List All Indices:

curl -X GET "localhost:9200/_cat/indices?v"

Delete an Index:

curl -X DELETE "localhost:9200/my_index"

Document Operations

Index a Document:

curl -X POST "localhost:9200/my_index/_doc/1" -d '{"field1": "value1", "field2": "value2"}'

Get a Document:

curl -X GET "localhost:9200/my_index/_doc/1"

Update a Document:

curl -X POST "localhost:9200/my_index/_doc/1/_update" -d '{"doc": {"field1": "new_value"}}'

Delete a Document:

curl -X DELETE "localhost:9200/my_index/_doc/1"

Search Operations

Simple Search:

curl -X GET "localhost:9200/my_index/_search?q=field1:value1"

Query DSL Search:

curl -X GET "localhost:9200/my_index/_search" -d '{"query": {"match": {"field1": "value1"}}}'

Aggregations

Terms Aggregation:

curl -X GET "localhost:9200/my_index/_search" -d '{"aggs": {"group_by_field": {"terms": {"field": "field1"}}}}'

Date Histogram Aggregation:

curl -X GET "localhost:9200/my_index/_search" -d '{"aggs": {"date_histogram": {"field": "timestamp", "interval": "day"}}}'

Bulk Operations

Bulk Indexing:

curl -X POST "localhost:9200/my_index/_bulk" -d '{"index": {"_id": "1"}}
{"field1": "value1", "field2": "value2"}
{"index": {"_id": "2"}}
{"field1": "value3", "field2": "value4"}'

Cluster Settings

Get Cluster Settings:

curl -X GET "localhost:9200/_cluster/settings"

Update Cluster Settings:

curl -X PUT "localhost:9200/_cluster/settings" -d '{"persistent": {"indices.recovery.max_bytes_per_sec": "50mb"}}'

This cheat sheet covers basic operations with Elasticsearch, including index, document, search, and cluster management. For more advanced features and configurations, refer to the Elasticsearch documentation.