Elasticsearch 9 cheatsheet
Elasticsearch 9: Indices and mappings
6 common Elasticsearch 9 tasks.
Create an index
Create an index with explicit settings.
curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt -X PUT "https://localhost:9200/products" -H "Content-Type: application/json" -d '{"settings":{"number_of_shards":1,"number_of_replicas":1}}'
Define field types
Add a mapping so fields are analyzed the way you want.
curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt -X PUT "https://localhost:9200/products/_mapping" -H "Content-Type: application/json" -d '{"properties":{"name":{"type":"text"},"sku":{"type":"keyword"},"price":{"type":"float"}}}'
Read a mapping
Show the field types of an index.
curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt "https://localhost:9200/products/_mapping?pretty"
Add an alias
Point an alias at an index, so applications don't depend on the index name.
curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt -X POST "https://localhost:9200/_aliases" -H "Content-Type: application/json" -d '{"actions":[{"add":{"index":"products-v1","alias":"products"}}]}'
Copy documents to a new index
Copy documents from one index to another, for example after changing a mapping.
curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt -X POST "https://localhost:9200/_reindex" -H "Content-Type: application/json" -d '{"source":{"index":"products-v1"},"dest":{"index":"products-v2"}}'
Delete an index
Delete an index and all its documents.
curl -u elastic:$ELASTIC_PASSWORD --cacert http_ca.crt -X DELETE "https://localhost:9200/products"
Caution: Permanently deletes the index. Check the name, since wildcards can match many indices.