How do you create an index in Elasticsearch?

Answer

You create an index in Elasticsearch using a PUT request to the REST API: PUT /my-index. You can optionally pass a JSON body with settings (number of shards and replicas) and mappings (field type definitions). For example: PUT /products { "settings": { "number_of_shards": 1, "number_of_replicas": 1 }, "mappings": { "properties": { "name": { "type": "text" }, "price": { "type": "float" } } } }. If you index a document into a non-existent index, Elasticsearch creates it automatically using dynamic mapping — though explicit creation is recommended in production for predictable behavior.