0
0
Elasticsearchquery~5 mins

Why indexes organize data in Elasticsearch

Choose your learning style9 modes available
Introduction

Indexes help organize data so you can find information quickly and easily.

When you want to search large amounts of text fast.
When you need to filter data by different categories.
When you want to analyze data trends over time.
When you want to store and retrieve data efficiently.
When you want to combine data from many sources for searching.
Syntax
Elasticsearch
PUT /my-index
{
  "settings": {
    "number_of_shards": 1,
    "number_of_replicas": 1
  },
  "mappings": {
    "properties": {
      "field1": { "type": "text" },
      "field2": { "type": "keyword" }
    }
  }
}

This example creates an index named my-index.

Settings control how data is stored and copied for safety.

Examples
Searches the my-index for documents where field1 matches "search text".
Elasticsearch
GET /my-index/_search
{
  "query": {
    "match": {
      "field1": "search text"
    }
  }
}
Deletes the entire index and all its data.
Elasticsearch
DELETE /my-index
Sample Program

This program creates an index called library with fields for book title, author, and year. Then it adds one book document. Finally, it searches for books with "Elasticsearch" in the title.

Elasticsearch
PUT /library
{
  "settings": {
    "number_of_shards": 1
  },
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "author": { "type": "keyword" },
      "year": { "type": "integer" }
    }
  }
}

POST /library/_doc
{
  "title": "Learn Elasticsearch",
  "author": "Jane Doe",
  "year": 2024
}

GET /library/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}
OutputSuccess
Important Notes

Indexes split data into pieces called shards to work faster.

Mapping defines how data fields are stored and searched.

Choosing the right field types helps find data more accurately.

Summary

Indexes organize data to make searching fast and easy.

They store data in a way that helps find matches quickly.

Settings and mappings control how data is saved and searched.