0
0
Elasticsearchquery~5 mins

Creating an index in Elasticsearch

Choose your learning style9 modes available
Introduction

Creating an index in Elasticsearch helps organize and store your data so you can search it quickly and easily.

When you want to store a new set of documents for searching.
When starting a new project that needs fast text search.
When you want to organize data by categories or types.
When you need to improve search speed by structuring data.
When preparing data for analytics or reporting.
Syntax
Elasticsearch
PUT /index_name
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "field1": { "type": "text" },
      "field2": { "type": "keyword" }
    }
  }
}
Use PUT method to create a new index with a chosen name.
You can define settings like shards and replicas, and mappings to specify data types.
Examples
This creates an index named books with fields for title, author, and year.
Elasticsearch
PUT /books
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "author": { "type": "keyword" },
      "year": { "type": "integer" }
    }
  }
}
This creates an index named movies with fields for name, genre, and release date.
Elasticsearch
PUT /movies
{
  "mappings": {
    "properties": {
      "name": { "type": "text" },
      "genre": { "type": "keyword" },
      "release_date": { "type": "date" }
    }
  }
}
Sample Program

This query creates an index called library with three fields: title, author, and published_year. It uses 1 shard and no replicas for simplicity.

Elasticsearch
PUT /library
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 0
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "author": { "type": "keyword" },
      "published_year": { "type": "integer" }
    }
  }
}
OutputSuccess
Important Notes

Index names must be lowercase and cannot contain spaces.

Shards split your data for faster search; replicas provide copies for safety.

Mapping defines how each field is stored and searched.

Summary

Creating an index organizes your data for fast searching.

Use PUT /index_name with settings and mappings to create it.

Choose field types carefully to match your data.