elasticsearch-query-optimization
Diagnose slow Elasticsearch Query DSL searches and propose measured fixes.
Skill by elasticOfficial
Install
Works with Claude Code, Claude apps, Codex, Cursor, Gemini CLI, GitHub Copilot, VS Code, OpenCode, Goose, Windsurf. Read the repository before you give any tool access to your data.
npx agentmarket install skill/elastic-elasticsearch-query-optimization-f95df3
Asks which of your tools to add it to (Claude Code, Codex, Cursor, VS Code, Windsurf, Gemini CLI, Claude Desktop) and shows the change first. See https://agentmart.dev/cli.
Get the weekly diff
The weekly diff: one email a week with new agent tools, notable updates and grade changes, plus security alerts for tools you follow. Double opt-in, one-click unsubscribe.
About
Reviews
Write a reviewNo reviews yet. Installed it? Reviews come from verified installers only. How reviews work
Permissions: not checked yet.
What's inside
- Skill name
elasticsearch-query-optimization- Description
- Diagnose slow Elasticsearch Query DSL searches and propose measured fixes. Use when a search is slow, profile output shows an expensive clause, exact-match filters sit in scoring context, or leading wildcards dominate latency. Ground every recommendation in search profiling — move non-scoring clauses to filter context, eliminate leading wildcards, and re-profile to confirm improvement.
- License
- Apache-2.0
- Compatibility
- 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.
- Metadata
- author: elastic · version: 0.1.0 · universal: true
- Instructions
- 187 lines in SKILL.md · view on GitHub
Files (2)
- SKILL.md
- query-optimization-reference.md · references/
SKILL.md (first lines)
# 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](https://github.com/elastic/cli#configuration) 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](#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 `WildcardQuery` at ~3.8s). Fix the query first; infrastructure changes require evidence the query
> is already optimal.
## Process
1. **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 example `logs-*`), narrow candidates with
`GET /_cat/indices` and 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).
2. **Profile the slow query to find the dominant cost.** Call `POST /{index}/_search` with `"profile": true` and the
user's query unchanged. Read `took`, then inspect `profile.shards[].searches[].query` — sort child collectors by
`time_in_nanos` and identify the top contributor.
**Decision:** classify the bottleneck from profile evidence:
- **`TermQuery` / `PointRangeQuery` / `MatchNoDocsQuery` inside `must` alongside a scoring clause** — exact-match or
range filters are being scored unnecessarily. Likely fix: move them to `filter` context (step 4a).
- **`WildcardQuery` with a leading `*` (for example `message:*timeout*`)** — cannot use the inverted index; scans
terms per document. Likely fix: remove the leading wildcard (step 4b).
- **`MatchQuery` on a `text` field** — expected scoring cost; optimize only if profile shows it dominates _after_
filter-context fixes.
- **High `aggregation` time** — 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`, and `breakdown` (especially `next_doc` for
wildcards). Quote the top contributor verbatim when explaining the diagnosis.
3. **Inspect field mappings before rewriting.** Call `GET /{index}/_mapping`. For every clause you will move or rewrite,
confirm the field type:
- **`term` / `terms` / `filter` on exact values** — field must be `keyword` (or another non-analyzed type). A `term`
on a `text` field is a common bug; if types are wrong, say so and suggest the correct sub-field (for example
`service.keyword`) or a mapping change — do not silently rewrite.
- **`match` / `match_phrase`** — target a `text` field (analyzed).
- **`wildcard`** — works on `keyword` or `wildcard` types; 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.
4. **Rewrite the query to remove the profiled bottleneck.**
### 4a. Move non-scoring clauses from `must` to `filter`
When exact-match `term`/`terms`/`range`/`match` on a keyword (or other non-scoring intent) clauses sit in `must`
alongside a full-text `match` that should drive relevance:
- Move exact-match clauses into `bool.filter` (or a `filter` array entry).
- Keep only clauses that must affect `_score` in `bool.must` (typically the full-text `match`).
**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:
```json
{
"query": {
"bool": {
"filter": [{ "term": { "status": "active" } }, { "term": { "tenant_id": "acme" } }],
"must": [{ "match": { "description": "wireless keyboard" } }]
}
}
}
```
### 4b. Eliminate leading wildcards
When the profile shows `WildcardQuery` with `description` like `message:*timeout*` and high `next_doc` time, 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 | `match` or `match_phrase` on the analyzed `message` `text` field |
| Literal substring on keyword | `wildcard`-typed field, or reindex with ngram analyzer |
| Prefix only (`timeout*`) | `prefix` query on `keyword`, or edge ngram at index time |
Also move any non-scoring exact match (for example `{ "match": { "service": "checkout" } }` on a keyword) into
`filter` — use `term` on the keyword field when the mapping confirms it.
Example rewrite pattern:
```json
{
"query": {
"bool": {
"filter": [{ "term": { "service.keyword": "checkout" } }],
"must": [{ "match": { "message": "timeout" } }]
}
}
}
```
Adjust field names (`service` vs `service.keyword`) to match the mapping from step 3.
### 4c. Optional — validate rewrite before profilingFrom elastic/agent-skills, Apache-2.0.
More from elastic/agent-skills
- 0.762score
elasticsearch-anomaly-detection
elasticOfficial
Create and manage Elastic ML anomaly detection jobs via the API.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-anomaly-detection-explainer
elasticOfficial
Explain Elasticsearch ML anomaly detection scores, model behavior, and result interpretation.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-esql
elasticOfficial
Execute ES|QL (Elasticsearch Query Language) queries, use when the user wants to query Elasticsearch data, analyze logs, aggregate metrics, explore data, or…
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-index-design
elasticOfficial
Design and review Elasticsearch index mappings for stated access patterns: correct field types, text+keyword multi-fields, doc_values tuning…
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-ingest
elasticOfficial
Load CSV and JSON files into Elasticsearch indices using the bulk API and explicit mappings when field types matter.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-onboarding
elasticOfficial
Help developers new to Elasticsearch get from zero to a working search experience.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-query-optimization
elasticOfficial
Diagnose slow Elasticsearch Query DSL searches and propose measured fixes.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-reindex
elasticOfficial
Guide Elasticsearch reindex for performance: local and remote, slicing, throttling, task API.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-search-relevance
elasticOfficial
Improve Elasticsearch search relevance for content and catalog indices: pin or promote results with query rules (correct rule type, criteria, and rule-query…
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
kibana-agent-builder
elasticOfficial
Create and manage Kibana Agent Builder agents and custom tools.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
kibana-alerting-rules
elasticOfficial
Create and manage Kibana alerting rules.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
kibana-anomaly-detection
elasticOfficial
Elastic ML anomaly detection — investigation/RCA, score explanation, job lifecycle troubleshooting, and job operations.
SkillBFreshness: B★ 600Claude Code +9
Signals
- Stars
- 600
- Forks
- 55
- Open issues
- 4
- Last commit (this folder)
- Sep 4, 2026 (1 month ago)
- Last release
- Sep 4, 2026 (1 month ago)
From GitHub (fetched Oct 24, 2026).
Details
- Version
- 0.1.0
- License
- Apache-2.0
- Repository
- github.com/elastic/agent-skills
- First seen
- Oct 24, 2026
- Last checked
- Oct 24, 2026
- Source
- GitHubgithub-graphql
Same skill elsewhere
Copies of this SKILL.md published by others; search shows only one of them.
More from elastic
- 0.762score
elasticsearch-anomaly-detection
elasticOfficial
Create and manage Elastic ML anomaly detection jobs via the API.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-anomaly-detection-explainer
elasticOfficial
Explain Elasticsearch ML anomaly detection scores, model behavior, and result interpretation.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-esql
elasticOfficial
Execute ES|QL (Elasticsearch Query Language) queries, use when the user wants to query Elasticsearch data, analyze logs, aggregate metrics, explore data, or…
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-index-design
elasticOfficial
Design and review Elasticsearch index mappings for stated access patterns: correct field types, text+keyword multi-fields, doc_values tuning…
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-ingest
elasticOfficial
Load CSV and JSON files into Elasticsearch indices using the bulk API and explicit mappings when field types matter.
SkillBFreshness: B★ 600Claude Code +9 - 0.762score
elasticsearch-onboarding
elasticOfficial
Help developers new to Elasticsearch get from zero to a working search experience.
SkillBFreshness: B★ 600Claude Code +9
Cite this page
elasticsearch-query-optimization skill. Agent Mart. https://agentmart.dev/skills/elastic-elasticsearch-query-optimization-f95df3 (last updated 2026-10-24)