0
0
ElasticsearchConceptBeginner · 3 min read

What is Index in Elasticsearch: Definition and Usage

In Elasticsearch, an index is like a database that stores and organizes documents for fast search and retrieval. It holds data in a structured way, allowing Elasticsearch to quickly find relevant information when you run queries.
⚙️

How It Works

Think of an index in Elasticsearch as a big, organized filing cabinet where each drawer holds documents related to a specific topic. When you add data, Elasticsearch stores it in this cabinet, making it easy to find later.

Each document inside the index is like a file with information, and Elasticsearch creates an internal map (called an inverted index) to quickly locate words or values inside these documents. This is similar to how a book's index helps you find topics by page number.

Because of this structure, Elasticsearch can search through large amounts of data very fast, returning results almost instantly.

💻

Example

This example shows how to create an index named library and add a document representing a book.

json
PUT /library
{
  "mappings": {
    "properties": {
      "title": { "type": "text" },
      "author": { "type": "keyword" },
      "year": { "type": "integer" }
    }
  }
}

POST /library/_doc/1
{
  "title": "The Great Gatsby",
  "author": "F. Scott Fitzgerald",
  "year": 1925
}

GET /library/_search
{
  "query": {
    "match": { "title": "Gatsby" }
  }
}
Output
{ "hits": { "total": {"value":1,"relation":"eq"}, "hits": [ { "_index": "library", "_id": "1", "_source": { "title": "The Great Gatsby", "author": "F. Scott Fitzgerald", "year": 1925 } } ] } }
🎯

When to Use

Use an index in Elasticsearch whenever you want to store and search large amounts of text or structured data quickly. For example, you can use indexes for:

  • Storing product catalogs for e-commerce sites to enable fast search by name or category.
  • Logging systems where you need to search through millions of log entries efficiently.
  • Content management systems to quickly find articles or documents by keywords.

Indexes help organize data so Elasticsearch can return search results in milliseconds, even with huge datasets.

Key Points

  • An index is a collection of documents in Elasticsearch.
  • It works like a database optimized for search.
  • Indexes use an inverted index structure to speed up queries.
  • You create indexes to organize and store data for fast retrieval.
  • Each index can have mappings that define the data types of fields.

Key Takeaways

An index in Elasticsearch stores and organizes documents for fast search.
It uses an inverted index to quickly find data inside documents.
Create indexes to manage and search large datasets efficiently.
Indexes have mappings that define the structure of stored data.
Use indexes for applications needing quick text or data search.