Why mappings define document structure in Elasticsearch - Performance Analysis
When we define mappings in Elasticsearch, we set rules for how data is stored and searched.
We want to understand how these rules affect the work Elasticsearch does as data grows.
Analyze the time complexity of defining and using mappings in Elasticsearch.
PUT /my_index
{
"mappings": {
"properties": {
"name": { "type": "text" },
"age": { "type": "integer" },
"tags": { "type": "keyword" }
}
}
}
This code sets the structure for documents by defining field types before indexing data.
When indexing documents, Elasticsearch applies the mapping rules repeatedly.
- Primary operation: Parsing each document field according to its mapping type.
- How many times: Once for every document and each field inside it.
As you add more documents, Elasticsearch repeats the parsing work for each one.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Parsing 10 documents × fields |
| 100 | Parsing 100 documents × fields |
| 1000 | Parsing 1000 documents × fields |
Pattern observation: The work grows directly with the number of documents you add.
Time Complexity: O(n)
This means the time to process documents grows in a straight line as you add more documents.
[X] Wrong: "Mappings only affect the first document, so adding more documents won't add work."
[OK] Correct: Every document must be checked against the mapping, so work grows with each new document.
Understanding how mappings affect processing helps you explain how Elasticsearch handles data efficiently as it grows.
"What if we add nested objects in mappings? How would that change the time complexity when indexing documents?"