Skill v1.0.0
currentAutomated scan100/100schema: "1.0" name: elasticsearch version: "1.0.0" description: Elasticsearch mapping design, sharding strategy, and query DSL performance practices domain: technology triggers: keywords: primary: [elasticsearch, elastic, opensearch, kibana, lucene] secondary: [mapping, shard, index, aggregation, analyzer, query DSL] context_boost: [full-text search, log search, search index] context_penalty: [splunk, postgresql, mongodb] priority: high dependencies: software-skills: [] author: claude-domain-skills
Elasticsearch
Mappings are mostly permanent and shard count is a day-one decision — plan before the first document lands
Applicable Scenarios
- Designing index mappings and choosing analyzers for search fields
- Deciding shard count and sizing strategy for a new index
- Writing or optimizing Query DSL queries and aggregations
- Diagnosing yellow/red cluster health or slow queries
- Handling deep pagination or large result set retrieval
Core Knowledge
The Inverted Index
- Elasticsearch (built on Lucene) indexes text by breaking it into terms via an analyzer (tokenizer + filters) and building an inverted index — term → list of documents containing it — which is what makes full-text search fast
- Mapping defines each field's type and how it's analyzed; unlike a relational schema, mapping is largely set at index-creation/first-document time — most field type changes require reindexing into a new index, not an in-place
ALTER
Sharding
- An index is split into primary shards (fixed at index creation in most versions — cannot be changed without reindexing) and replica shards (copies for HA and read scaling, changeable anytime)
- Shard count is a capacity-planning decision made once: too few shards limits horizontal scaling and can make shards unmanageably large; too many creates per-shard overhead (cluster state size, file handles, per-shard query overhead) that degrades cluster performance even with little data
- A good starting heuristic: size shards to roughly 10-50GB each based on expected index size, not a round number picked arbitrarily
Query DSL: Query Context vs. Filter Context
| Context | Purpose | Caching | Scoring | |
|---|---|---|---|---|
| Query context | "How well does this match?" | Not cached | Computes a relevance score | |
| Filter context | "Does this match, yes/no?" | Cached by Elasticsearch | No scoring — cheaper |
Using query-context clauses (e.g., unwrapped match) for what's actually a binary filter (exact term match, date range) wastes scoring computation and misses out on filter caching — wrap exact-match conditions in a bool.filter clause instead of bool.must.
Aggregations
- Aggregations compute over the matched document set (after query/filter), similar conceptually to SQL
GROUP BY— but computed in a distributed way across shards, which means very high-cardinality aggregations (e.g., aggregating on a unique ID field) are expensive and memory-intensive termsaggregations return approximate results by default in some multi-shard configurations if not enough data is fetched per shard (controlled byshard_size) — accuracy vs. cost is a real tradeoff, not automatic
Pagination
from/sizepagination becomes expensive and eventually blocked past a default limit (index.max_result_window, default 10,000) because deep pagination requires sorting and discarding all preceding results on every shardsearch_after(cursor-based, using the last result's sort values) or the Point-in-Time +search_aftercombination scales to arbitrary depth without the same overhead
Best Practices
- Define explicit mappings rather than relying on dynamic mapping for anything beyond quick prototyping — dynamic mapping can silently create unexpected fields and types
- Size shards deliberately (roughly 10-50GB target) based on expected data volume, not a default or arbitrary round number
- Use filter context for exact-match/range conditions, query context only for actual relevance-scored full-text matching
- Use `search_after` for deep pagination, not
from/sizepast a shallow depth - Set an index lifecycle policy (ILM) for time-series/log data so old indices roll over, get downgraded to cheaper storage tiers, or get deleted automatically
- Monitor cluster health status (green/yellow/red) and unassigned shard count as standard operational metrics
Common Mistakes
| Mistake | Correct Approach | |
|---|---|---|
| Relying on dynamic mapping for production indices | Define explicit mappings, disable or restrict dynamic mapping (dynamic: strict or false) where field shape should be controlled | |
Using bool.must with match for an exact-value filter | Use bool.filter for exact matches/ranges — cached, no scoring overhead | |
from/size pagination going deep into results | Use search_after with a Point-in-Time for deep pagination | |
| One shard per index regardless of expected data size | Size shards to the expected data volume (~10-50GB per shard as a starting point) | |
| No ILM policy on time-series/log indices | Configure ILM to roll over, tier, and delete old indices automatically |
Sharp Edges
SE-1: Dynamic Mapping Explosion
- Severity: critical
- Situation: An index with dynamic mapping enabled ingests documents with highly variable or attacker-influenced field names (e.g., using a user-controlled key as a JSON field name), and the mapping accumulates thousands of unique fields, destabilizing the cluster
- Cause: Dynamic mapping automatically creates a new mapped field for every previously-unseen field name — there's no default cap, and cluster state (which must be replicated to every node) grows with total mapped field count across all indices
- Symptoms:
- Cluster state size grows abnormally large; cluster state updates become slow, affecting the whole cluster's responsiveness
mapper_parsing_exceptionorillegal_argument_exception: Limit of total fields ... has been exceededonce the field cap (default 1000) is hit- Solution: Set
dynamic: strictorfalseon indices where field shape should be controlled, use explicit mappings for known fields, and if arbitrary key-value data must be stored, use aflattenedfield type (indexes as a single field, not one mapped field per key) instead of relying on dynamic object mapping - Details: → [extended/checklists.md#index-design-checklist]
SE-2: Over-Sharding Degrading Cluster Performance
- Severity: high
- Situation: Many small indices (e.g., a new index created per day/tenant with default shard settings) accumulate thousands of tiny shards across the cluster, and cluster-wide operations (cluster state updates, shard allocation) become slow even though total data volume is modest
- Cause: Every shard has fixed per-shard overhead (file handles, memory for shard-level data structures, participation in cluster state) — this overhead is roughly constant per shard regardless of how much data it actually holds, so many small shards accumulate overhead disproportionate to the data they store
- Symptoms:
- Cluster health checks and shard allocation operations become slow
too_many_requests/ circuit breaker exceptions during normal operation despite modest total data size- Solution: Right-size shard count for expected data volume (10-50GB per shard target), use index templates with a rollover policy to control shard proliferation, and periodically use
_shrink/reindex to consolidate over-sharded indices
SE-3: Deep Pagination Exhausting Memory
- Severity: high
- Situation: An application paginating deep into search results using
from/size(e.g.,from: 50000, size: 10) causes high memory usage and latency, and eventually hits the default 10,000-result window limit - Cause:
from/sizepagination requires each shard to sort and returnfrom + sizeresults to the coordinating node, which then merges and discards everything beforefrom— the cost of retrieving "page 5000" is proportional tofrom, not to the page size - Symptoms:
- Query latency increases the further into results pagination goes
Result window is too largeerrors oncefrom + sizeexceedsindex.max_result_window- Solution: Use
search_after(with a stable sort and, for point-in-time consistency, a PIT ID) for any pagination that needs to go beyond a shallow depth — it scales with page size, not absolute offset
SE-4: Immutable Field Types Requiring Full Reindex
- Severity: medium
- Situation: A field mapped as
textneeds to becomekeyword(or any other type change) for a new query pattern, and there's no in-place way to do it — the only path is creating a new index with the corrected mapping and reindexing all existing data into it - Cause: Lucene segments are immutable once written; changing a field's fundamental type would require rewriting every existing segment, which Elasticsearch doesn't support as an in-place operation
- Symptoms:
PUTmapping request for an existing field's type change fails with a mapper conflict error- A schema fix requires standing up a parallel index and a reindex/cutover process rather than a quick change
- Solution: Plan mappings carefully upfront (use
keywordfor exact-match/aggregation fields,textfor full-text search, andmulti-fieldsto index the same value both ways when both access patterns are needed), and use an alias pointing at the current index so reindex-and-cutover doesn't require an application-level change
SE-5: Query Context Used Where Filter Context Belongs
- Severity: medium
- Situation: A query using
bool.mustwith an exact-matchterm/matchclause for something that's really a binary filter (e.g.,status: "active") computes and sums a relevance score unnecessarily, and misses out on Elasticsearch's filter result caching, on every single request - Cause: Query context always computes a
_score; filter context is cached and skips scoring — but nothing prevents writing an exact-match condition insidemust(query context) when it should be infilter - Symptoms:
- Aggregate query latency across a cluster is higher than expected for the query complexity involved
- Filter-like clauses (exact term matches, ranges) appear inside
bool.mustrather thanbool.filterin query review - Solution: Move exact-match and range conditions that don't need relevance scoring into
bool.filter, reservingbool.must/shouldfor genuinely relevance-scored full-text matching
Recommended Tools
| Category | Tools | |
|---|---|---|
| Query analysis | _explain API, Kibana Dev Tools, Profile API | |
| Cluster management | _cluster/health, _cat/shards, ILM policies | |
| Ingestion | Logstash, Beats, ingest pipelines | |
| Client libraries | Official Elasticsearch clients per language |
Index Design
Full checklist: → [extended/checklists.md#index-design-checklist]
Related Resources
Elasticsearch Docs | Elasticsearch: The Definitive Guide | Sizing Elasticsearch
Related Domains
[[splunk]] | [[kafka]] | [[python]] | [[java]]