What is Index in Elasticsearch: Definition and Usage
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.
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" }
}
}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
indexis 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.