</>

Elasticsearch Cheat Sheet

Practical Elasticsearch cheat sheet with setup steps, core workflows, debugging, and copy-paste examples.

elasticsearch queries schema performance

Elasticsearch cheat sheet with real commands and snippets for setup, core workflows, debugging, and production-safe automation patterns. If you are working across tools, pair this with the MongoDB Cheat Sheet and SQL Cheat Sheet.

Setup and Connection

Goal: Connect to database shell or endpoint

# Run connection or version command
curl -s http://localhost:9200

# Run connection or version command
curl -s http://localhost:9200/_cat/indices?v

# Run connection or version command
curl -s http://localhost:9200/_cluster/health?pretty

Schema and Data Modeling

Goal: Create a table/index/schema foundation

# Create index with explicit mappings for reliable queries
curl -X PUT 'http://localhost:9200/logs' -H 'Content-Type: application/json' -d '{
  "mappings": {
    "properties": {
      "level": { "type": "keyword" },
      "message": { "type": "text" },
      "created_at": { "type": "date" }
    }
  }
}'

Core Queries and Mutations

Goal: Run practical read/write operations

# Search logs by text and date range
curl -X GET 'http://localhost:9200/logs/_search' -H 'Content-Type: application/json' -d '{
  "query": {
    "bool": {
      "must": [{ "match": { "message": "timeout" }}],
      "filter": [{ "range": { "created_at": { "gte": "now-7d" }}}]
    }
  }
}'

Performance and Optimization

Goal: Add indexes and inspect query plans

# Inspect index stats and shard allocation
curl -s http://localhost:9200/logs/_stats?pretty
curl -s http://localhost:9200/_cat/shards/logs?v

Automation and Backups

Goal: Create backup and restore routines

# Export data snapshot before risky migrations
pg_dump "$DATABASE_URL" > backup.sql || mysqldump appdb > backup.sql || true

# Compress backup artifact for storage
gzip -f backup.sql || true

# Verify backup file exists and has content
ls -lh backup.sql.gz || ls -lh backup.sql

Debugging and Troubleshooting

Goal: Inspect slow-query symptoms

# Check active connections/sessions
psql -c "SELECT now();" || mysql -e "SELECT NOW();" || true

# Inspect query plans for heavy statements
psql -c "EXPLAIN SELECT 1;" || mysql -e "EXPLAIN SELECT 1;" || true

# Search migration files for destructive statements
rg "DROP TABLE|TRUNCATE|DELETE FROM" prisma migrations sql -n

Common Gotchas

  • Use transactions for multi-step Elasticsearch data changes that must stay consistent.
  • Create indexes for frequent WHERE and JOIN columns, then verify with query plans.
  • Back up production data before schema migrations.
  • Separate read-only credentials from write credentials in app environments.
  • Monitor connection pool limits to avoid timeout spikes under load.

Related Cheat Sheets