Elasticsearch Query Optimization
ElasticOfficialCreated by elastic
- Skill
- Workflow
- Software development
- Apache-2.0
- No issues found
AutoGineer summary
Written by AutoGineer from the skill’s own files, not by the author
This skill provides a structured methodology for identifying and resolving performance bottlenecks in Elasticsearch queries. By leveraging the search profiling API, users can move beyond guesswork to identify expensive query clauses, such as unnecessary scoring filters or inefficient leading wildcards. The workflow emphasizes data-driven optimization, requiring users to profile the baseline, implement targeted rewrites—such as moving clauses to filter context—and re-profile to verify performance gains.
What it does
- Diagnoses slow Query DSL performance using the Elasticsearch profiling API.
- Identifies expensive clauses like leading wildcards or unnecessary scoring filters.
- Provides patterns for moving exact-match clauses from must to filter context.
- Offers strategies to replace inefficient leading wildcards with optimized alternatives.
- Validates query rewrites using the validate-query API.
- Requires measured before-and-after comparisons to confirm optimization success.
When to use it
- When a search query is performing slowly and requires optimization.
- When profile output indicates expensive clauses dominating query latency.
- When exact-match filters are incorrectly placed in the scoring context.
- When leading wildcards are causing high latency due to full-term scans.
Advantages
- Grounds all optimization recommendations in objective profiling data.
- Improves performance by leveraging the filter cache for exact-match clauses.
- Reduces query latency by eliminating inefficient wildcard scanning.
- Preserves search semantics while optimizing execution efficiency.
Usage examples
- “My search is slow, how do I optimize it?”
- The agent will guide you through running a profiled search, identifying the top-cost collector in the profile tree, and rewriting the query to move non-scoring filters to the filter context.
- “Why is my wildcard query taking 5 seconds?”
- The agent will analyze the profile to confirm if a leading wildcard is causing a full-term scan, then suggest replacing it with a match or match_phrase query on an analyzed field.
Install
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/elastic/elasticsearch-query-optimization/downloadUnzip it into the cross-client skills folder of your project:
mkdir -p .agents/skills && unzip elasticsearch-query-optimization.zip -d .agents/skills
Documentation by elastic
Elasticsearch Query DSL Optimization
Diagnose why a Query DSL search is slow, identify the dominant cost from the profile (not guesswork), rewrite the query to remove that cost while preserving match semantics, and re-measure with profiling enabled.
<!-- begin-partial: preamble -->
Environment Configuration
This skill executes Elasticsearch operations through the elastic CLI. If the
elastic CLI is not installed, tell the user what it is needed for. Do
not guess credentials, call the HTTP API directly, or attempt other workarounds.
This skill references operations in HTTP-shorthand form (e.g., GET /, GET /_cat/indices, GET /{index}/_mapping,
GET /{index}/_settings/index.mode, POST /_query). The Operations table at the end of this document
maps each shorthand to the equivalent elastic CLI command — always use the CLI rather than calling the HTTP API
directly.
<!-- end-partial: preamble -->
Scope: Query DSL searches via
POST /{index}/_search. This skill does not migrate queries to ES|QL — it optimizes the existing bool/match/term/wildcard structure the user already runs.Ground rule: Never recommend "add shards" or "scale hardware" as the primary fix when the profile names a specific clause (for example
WildcardQueryat ~3.8s). Fix the query first; infrastructure changes require evidence the query is already optimal.
Process
Confirm connectivity and locate the target index. Call
GET /. If the call fails, stop — do not guess endpoints or credentials. When the user names an index pattern (for examplelogs-*), narrow candidates withGET /_cat/indicesand pick the index or pattern the query actually targets.Decision: proceed only when the index is known. Data needed: index name or pattern, and the slow Query DSL body (from the user or from a saved search).
Profile the slow query to find the dominant cost. Call
POST /{index}/_searchwith"profile": trueand the user's query unchanged. Readtook, then inspectprofile.shards[].searches[].query— sort child collectors bytime_in_nanosand identify the top contributor.Decision: classify the bottleneck from profile evidence:
TermQuery/PointRangeQuery/MatchNoDocsQueryinsidemustalongside a scoring clause — exact-match or range filters are being scored unnecessarily. Likely fix: move them tofiltercontext (step 4a).WildcardQuerywith a leading*(for examplemessage:*timeout*) — cannot use the inverted index; scans terms per document. Likely fix: remove the leading wildcard (step 4b).MatchQueryon atextfield — expected scoring cost; optimize only if profile shows it dominates after filter-context fixes.High
aggregationtime — separate from query tuning; profile the agg tree (out of scope unless the user asked about aggs).
Data needed: profile tree with
type,description,time_in_nanos, andbreakdown(especiallynext_docfor wildcards). Quote the top contributor verbatim when explaining the diagnosis.Inspect field mappings before rewriting. Call
GET /{index}/_mapping. For every clause you will move or rewrite, confirm the field type:term/terms/filteron exact values — field must bekeyword(or another non-analyzed type). Atermon atextfield is a common bug; if types are wrong, say so and suggest the correct sub-field (for exampleservice.keyword) or a mapping change — do not silently rewrite.match/match_phrase— target atextfield (analyzed).wildcard— works onkeywordorwildcardtypes; leading*still forces a scan regardless of type.
Decision: only propose rewrites that match confirmed types. Data needed: mapping for each field referenced in the query.
Rewrite the query to remove the profiled bottleneck.
4a. Move non-scoring clauses from
musttofilterWhen exact-match
term/terms/range/matchon a keyword (or other non-scoring intent) clauses sit inmustalongside a full-textmatchthat should drive relevance:Move exact-match clauses into
bool.filter(or afilterarray entry).Keep only clauses that must affect
_scoreinbool.must(typically the full-textmatch).
Why: filter context skips scoring and participates in the filter/bitset cache on repeated queries. Semantics: the same documents match; only scoring and performance change — state this explicitly.
Example rewrite pattern:
{ "query": { "bool": { "filter": [{ "term": { "status": "active" } }, { "term": { "tenant_id": "acme" } }], "must": [{ "match": { "description": "wireless keyboard" } }] } } }4b. Eliminate leading wildcards
When the profile shows
WildcardQuerywithdescriptionlikemessage:*timeout*and highnext_doctime, the leading*prevents index lookup. Choose a fix based on mapping and user intent (substring vs prefix vs exact):Intent Preferred rewrite Full-text substring in logs matchormatch_phraseon the analyzedmessagetextfieldLiteral substring on keyword wildcard-typed field, or reindex with ngram analyzerPrefix only ( timeout*)prefixquery onkeyword, or edge ngram at index timeAlso move any non-scoring exact match (for example
{ "match": { "service": "checkout" } }on a keyword) intofilter— usetermon the keyword field when the mapping confirms it.Example rewrite pattern:
{ "query": { "bool": { "filter": [{ "term": { "service.keyword": "checkout" } }], "must": [{ "match": { "message": "timeout" } }] } } }Adjust field names (
servicevsservice.keyword) to match the mapping from step 3.4c. Optional — validate rewrite before profiling
When semantics are uncertain (for example changing
wildcardtomatchmay include analyzed tokens the wildcard excluded), callPOST /{index}/_validate/query?explain=truewith the rewritten query and read the explanation for obvious mismatches.Decision: pick the smallest rewrite that addresses the profiled cost. Data needed: rewritten Query DSL body.
Re-profile the rewritten query and compare. Call
POST /{index}/_searchagain with"profile": trueand the rewritten query. Comparetookand the top profile collector to the baseline from step 2.Decision: report success only when the dominant collector changed or
time_in_nanosdropped materially. If the profile still shows a leading wildcard or scored filters, iterate — do not declare victory fromtookalone without profile confirmation.Data needed: before/after profile summaries (top collector
type,description,time_in_nanos).Report findings in this order.
Root cause — quote the profile (for example "
WildcardQuerymessage:*timeout*≈ 3.8s, mostlynext_doc").Rewrite — show the optimized bool structure with filter vs must separation.
Mapping notes — keyword vs text confirmations from
GET /{index}/_mapping.Measured improvement — before/after profile or
tookfrom step 5.Semantic caveat — only if the rewrite could change which documents match (for example
matchvs substringwildcard).
Guidelines
Profile first. If the user supplies a profile summary, use it — but still recommend re-profiling after changes.
Filter is for equality, must is for relevance. Status, tenant ID, service name, and time ranges rarely belong in
mustwhen a text query drives ranking.Leading wildcards are almost never the right fix for log search. Prefer analyzed
match/match_phrase; reservewildcardfor suffix patterns (timeout*) on keyword orwildcard-typed fields.Do not conflate slow with wrong. A slow query can return correct results; optimization preserves the result set unless you explicitly warn about a semantic trade-off.
Deep reference: profile collector types, filter-cache behavior, and wildcard alternatives — references/query-optimization-reference.md.
Examples
Unscored terms in must
Input: bool.must contains term on status, term on tenant_id, and match on description.
Diagnosis: profile shows scored TermQuery collectors alongside MatchQuery; exact filters do not need scoring.
Fix: move both term clauses to filter; keep match in must. Confirm status and tenant_id are keyword.
Leading wildcard dominates latency
Input: wildcard message:*timeout* plus match on service in must. Profile: WildcardQuery ~3.8s.
Diagnosis: leading * forces term enumeration; not an index/shard problem.
Fix: match on analyzed message; move service to filter as term on keyword. Re-profile — expect
WildcardQuery to disappear or shrink to negligible time.
Operations
| HTTP API (shorthand) | elastic CLI command |
|---|---|
GET / | elastic es info |
GET /_cat/indices | elastic es cat indices --index '<pattern>' |
GET /{index}/_mapping | elastic es indices get-mapping --index '<index>' |
POST /{index}/_search | elastic es search --index '<index>' --input-file '<search-body.json>' |
POST /{index}/_validate/query?explain=true | elastic es indices validate-query --index '<index>' --explain true --query '<json>' |
Include "profile": true in the search JSON body (or pass --profile true) when profiling in steps 2 and 5.
Files
- ATTRIBUTION.mdAdded by AutoGineer1019 B
- LICENSELicence file added by AutoGineer11.1 KiB
- SKILL.md11.1 KiB
- references/query-optimization-reference.md5.2 KiB
references/query-optimization-reference.md
Query DSL Optimization Reference
Supporting detail for profile interpretation, query vs filter context, and wildcard alternatives.
Search profiling
Enable profiling with "profile": true on POST /{index}/_search. The response adds a profile object parallel to
hits.
Reading the query tree
Each shard returns profile.shards[].searches[].query — a tree of collectors. For each node, inspect:
| Field | Meaning |
|---|---|
type | Collector class (WildcardQuery, TermQuery, MatchQuery, BooleanQuery, …) |
description | Lucene description (for example message:*timeout*, status:active) |
time_in_nanos | Total time for this collector on this shard |
breakdown | Sub-timers; next_doc high on wildcards indicates per-doc scanning |
Triage rule: the collector with the highest time_in_nanos (summed across shards when comparing) is the primary
optimization target. Secondary collectors matter only after the top cost is addressed.
Common profile signatures
| Profile signal | Typical cause | First fix |
|---|---|---|
WildcardQuery + high next_doc + *term* pattern | Leading or infix wildcard | match/match_phrase, prefix, or ngram field |
Multiple TermQuery in must + one MatchQuery | Filters scored unnecessarily | Move terms to filter |
MatchQuery dominates after filter fix | Large candidate set or heavy analyzer | Narrow with filter; check analyzer |
BooleanQuery with many should | Disjunction max over many clauses | Reduce clauses; move constants to filter |
Profiling adds overhead — use it for diagnosis and before/after comparison, not on every production request.
Query context vs filter context
In a bool query:
| Context | Scoring | Caching | Use for |
|---|---|---|---|
must | Yes | No | Clauses that must match and affect score |
should | Yes | No | Optional relevance boosts |
filter | No | Yes (filter cache / bitset) | Exact match, ranges, non-scoring matches |
must_not | No | No (exclusion) | Exclusions |
Moving a term from must to filter:
Same matching documents when the clause is required (wrap in
boolwith the same logical AND).Different
_score— filter clauses do not contribute to score; the remainingmustclauses define ranking.Better repeat-query performance — identical filter clauses can reuse cached bitsets.
Wildcard and substring search
Why leading wildcards are slow
Lucene's inverted index maps terms → documents. A prefix or suffix wildcard (timeout*) can sometimes use the term
dictionary. A leading wildcard (*timeout*) cannot narrow the term set upfront; the engine scans many terms and visits
documents (next_doc in the profile).
Alternatives by requirement
| Requirement | Approach |
|---|---|
| Search analyzed log text | match or match_phrase on text field |
| Case-sensitive substring | wildcard-typed field (still costly for leading *; better than text) |
| Fast prefix autocomplete | Edge n-gram tokenizer at index time + match or prefix on keyword |
| Fast infix/substring at scale | N-gram analyzer (index-time cost; query-time match) |
| Known suffix on keyword | wildcard with pattern *suffix (no leading star on short prefix) |
When replacing wildcard *foo* with match, warn that analysis may tokenize differently (for example timeout vs
timeouts) — compare hit counts or use validate/query when precision matters.
Field type cheat sheet
| Query type | Expected mapping | Common mistake |
|---|---|---|
term | keyword, numeric, date | term on analyzed text |
match | text | Using match for exact ID equality |
wildcard | keyword, wildcard | Leading * on high-cardinality fields |
range | Numeric, date, keyword | Range on text |
Always confirm with GET /{index}/_mapping — ECS and custom schemas use different sub-field names (service vs
service.keyword).
Requirements and compatibility
Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta
“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.
The author says: Elasticsearch 8.x or 9.x, self-managed, Elastic Cloud Hosted, or Elastic Cloud Serverless; relies on the search profiling API available on all deployment types. Requires the `elastic` CLI ≥ 0.2 with `stack es` support.
Security
No issues found by our static scan· No issues found
A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.
Attribution and licence
- Created by
- elastic
- Publisher
- Elastic (Official)
- Source
- elastic/agent-skills · skills/elasticsearch/elasticsearch-query-optimization · commit 09d879d
- Licence
- Apache License 2.0 (redistribution allowed)
- LICENSE (added by AutoGineer from LICENSE)
- ATTRIBUTION.md (written by AutoGineer)
- Synced
- Also distributed in
- elastic/agent-skills
AutoGineer is not affiliated with or endorsed by elastic.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download elasticsearch-query-optimization.zip