Why indexes organize data in Elasticsearch - Performance Analysis
Indexes in Elasticsearch help organize data to make searching faster.
We want to know how the time to find data changes as the amount of data grows.
Analyze the time complexity of searching data using an index.
GET /my_index/_search
{
"query": {
"match": {
"title": "elasticsearch"
}
}
}
This code searches the "title" field in the "my_index" index for the word "elasticsearch".
When searching, Elasticsearch looks through the index structure.
- Primary operation: Traversing the index tree to find matching documents.
- How many times: The search moves down the tree levels, which depends on the index size.
As the number of documents grows, the index tree grows in height slowly.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 3 steps down the tree |
| 100 | About 7 steps down the tree |
| 1000 | About 10 steps down the tree |
Pattern observation: The steps grow slowly compared to the number of documents, making search efficient.
Time Complexity: O(log n)
This means the search time grows slowly as data grows, because the index helps find data quickly.
[X] Wrong: "Searching an index checks every document one by one."
[OK] Correct: The index organizes data so Elasticsearch jumps directly to relevant parts, not checking all documents.
Understanding how indexes speed up search shows you know how data organization affects performance, a key skill in many programming tasks.
"What if the index was not used and Elasticsearch searched all documents directly? How would the time complexity change?"