In Elasticsearch, an index provides structured storage and retrieval mechanisms by distributing documents across primary and replica shards. Proper index configuration ensures efficient data access and rapid response to queries.

Defining initial mappings at creation time shapes field analysis and indexing behavior, improving query performance, aggregations, and relevance. With flexible schema design, adjustments can be made as data and workload requirements evolve.

Setting the appropriate shard count, analyzers, and index settings establishes a strong foundation for scalable and high-performance search operations as data volumes grow.

Steps to create an index in Elasticsearch:

  1. Verify cluster health is stable (e.g., green) before proceeding.
  2. Ensure Elasticsearch is running and accessible on port 9200.
  3. Send a PUT request to create the index with desired settings.
    $ curl --request PUT --header "Content-Type: application/json" --data '{
      "settings": {
        "number_of_shards": 1,
        "number_of_replicas": 1
      }
    }' http://localhost:9200/my_index
    
    {"acknowledged":true,"shards_acknowledged":true,"index":"my_index"}

    Fewer shards may boost performance for smaller datasets.

  4. Verify the index creation.
    $ curl --request GET --silent http://localhost:9200/_cat/indices?v
    health status index     uuid                   pri rep docs.count ...
    green  open   my_index  AbCdEf123...           1   1    0 ...

    cat APIs provide human-readable cluster and index information.

  5. Optionally define mappings to specify field types and analyzers.
  6. Add documents to the index using POST or PUT requests.
  7. Refresh the index to make recently indexed documents immediately searchable.
    $ curl --request POST http://localhost:9200/my_index/_refresh
    {"_shards":{"total":1,"successful":1,"failed":0}}

    Frequent refreshes may degrade performance; use judiciously in production.

Discuss the article:

Comment anonymously. Login not required.