Elasticsearch Index

An Index in Elasticsearch functions as the primary logical namespace for storing structured JSON documents. While it shares conceptual similarities with a table in traditional relational databases, its underlying architecture is powered by Apache Lucene, delivering distributed, horizontally scalable, and near-real-time search capabilities. Mastering how to configure, map, shard, and maintain indices forms the absolute foundation of working effectively with the platform.

An index is not merely a static container; it represents a collection of documents that share a unified schema design. Within modern Elasticsearch ecosystems, the legacy multi-type concept has been entirely deprecated and removed, establishing the standard practice of utilizing _doc as the default endpoint while organizing data strictly around dedicated index boundaries.
To design robust search solutions, you must understand several fundamental building blocks:

  • Document: The basic unit of information in Elasticsearch, typically structured as a JSON object capable of being indexed, retrieved, and updated.
  • Mapping: The blueprint of an index that defines field names, data types, text analysis rules, and whether specific attributes should be searchable.
  • Shard: The physical subdivision of an index. Each shard operates as an independent Lucene instance distributed across cluster nodes.
  • Replica: Exact copies of primary shards designed to guarantee high availability and scale out read throughput.
  • Alias: A virtual pointer referencing one or more indices, abstracting physical index changes away from client applications.

Creating and Inspecting Indices

When initializing an index, you can explicitly configure cluster settings and field mappings simultaneously to optimize ingestion and search performance.

PUT /products
{
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 1,
    "refresh_interval": "1s"
  },
  "mappings": {
    "properties": {
      "name": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword",
            "ignore_above": 256
          }
        }
      },
      "price": {
        "type": "scaled_float",
        "scaling_factor": 100
      },
      "created_at": {
        "type": "date"
      },
      "tags": {
        "type": "keyword"
      }
    }
  }
}

To inspect your indices and verify their cluster status, use standard diagnostic endpoints:

  • GET /products/_mapping: Inspects the active field mappings.
  • GET /products/_settings: Reviews current cluster settings and allocation configurations.
  • GET /_cat/indices?v: Displays an overview of all cluster indices and health metrics.
  • HEAD /products: Rapidly verifies whether a specific index exists.

Mapping Design Principles

Mappings dictate how raw data is parsed, indexed, and evaluated during queries. Relying entirely on dynamic mapping can introduce unexpected performance overhead or schema bloat.

  • Text vs. Keyword: Use text fields for full-text searches requiring linguistic analysis, and keyword fields for exact matching, sorting, and aggregations.
  • Temporal Data: Leverage type: date to handle date parsing formats and range queries seamlessly.
  • Numeric Precision: Choose the appropriate numeric type (long, integer, double, or scaled_float) based on your exact precision requirements.
  • Excluding Fields: For metadata that never requires search capabilities, explicitly set "index": false to save disk space and processing cycles.

If your data schemas are prone to arbitrary structures, configure the dynamic parameter to false or strict to prevent runaway mapping explosions.

Document Operations and Queries

Interacting with documents involves standard RESTful verbs. You can provide a custom identifier or let the system auto-generate a unique ID.

PUT /products/_doc/1
{
  "name": "Wireless Bluetooth Earbuds",
  "price": 299.00,
  "created_at": "2024-05-01",
  "tags": ["electronics", "audio"]
}

Executing a full-text query against the index is straightforward:

GET /products/_search
{
  "query": {
    "match": {
      "name": "Bluetooth"
    }
  }
}

Modifying existing documents can be achieved via the _update API, while deletions target specific documents or entire indices using the DELETE verb.

Sharding and Replication Strategy

Primary shard allocations are immutable after index creation, making upfront capacity planning vital. Replica counts, however, can be adjusted dynamically on the fly:

PUT /products/_settings
{
  "number_of_replicas": 2
}

Key capacity planning guidelines include:

  • Keep individual shard sizes within a reasonable threshold (typically tens of gigabytes).
  • Maintain at least one replica to safeguard against node failures.
  • Avoid over-sharding, which introduces unnecessary cluster metadata overhead.
  • For time-series workloads, implement date-based naming conventions like logs-2024.05.01.

Aliases and Index Templates

Aliases provide an essential layer of abstraction, allowing client applications to interact with a stable endpoint while underlying physical indices rotate.

POST /_aliases
{
  "actions": [
    {
      "add": {
        "index": "products_v1",
        "alias": "products"
      }
    }
  ]
}

When zero-downtime reindexing is required, developers can populate a new index (products_v2) and atomically swap the alias in a single API call. Index templates further streamline operations by automatically applying predefined settings and mappings to newly matched indices.

Maintenance and Best Practices

  • Decouple applications from physical indices by consistently utilizing aliases.
  • Define explicit mappings to avoid unpredictable field types generated by dynamic mapping.
  • Leverage Index Lifecycle Management (ILM) to automate rollover, retention, and cold/hot storage tiering.
  • Utilize the _reindex API safely when structural data migrations are necessary.
  • Tune the refresh_interval upward during heavy ingestion phases to boost indexing speed, then lower it when search latency demands immediacy.
  • Continuously monitor index health and shard distribution using cluster management APIs.

Thoughtful index design guarantees sustained search performance, ensures predictable resource utilization, and minimizes long-term operational overhead.