Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

`es search --input-file` drops top-level `aggs` (and any unrecognised key) without an error

未关闭
#801 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@plinde 已经在做这个了。

开始于 2026年10月28日。

  • #803 来自 @plinde —— 未关闭

评估

难度
3/5
预计耗时
1-2 天
新手友好度
55/100
Issue 类型
缺陷
描述清晰度
描述清楚
活跃度
活跃
技术栈
elasticsearch, typescript
领域
cli

调研方向

Start by reproducing with the body.json snippet and comparing the CLI result with the direct Elasticsearch call. Then read src/es/request-builder.ts:163-169 and src/lib/json-schema-args.ts:193-206 to see how only schema-declared keys are copied, and check src/lib/ajv-validate.ts for validation behavior. Decide between forwarding and rejecting unknown top-level keys, and handle aggs as an alias of aggregations; done means aggs works and a misspelt key no longer silently returns unfiltered results, with tests for both.

由索引模型根据 Issue 内容生成。

描述

Related to elastic/cli#803

Summary

The ES request builder rebuilds the request body from the keys declared in the command's schema. Any other top-level key in --input-file is dropped before the request is sent. The command exits 0 and prints no warning.

The most common case is aggs. Elasticsearch accepts both aggs and aggregations, and most docs and examples use aggs. The search schema declares only aggregations, so a body written with aggs returns hits and no aggregations key.

A misspelt key fails the same way. Elasticsearch would answer 400 to a misspelt query; the CLI sends the request without it and returns unfiltered results.

Reproduction (v0.7.0, also unchanged on main at fd3359d)

cat > body.json <<'JSON'
{"size":0,"aggs":{"by_day":{"date_histogram":{"field":"@timestamp","calendar_interval":"day"}}}}
JSON

elastic es search --index my-index --input-file body.json | jq 'has("aggregations")'
# false   (exit 0, empty stderr)

curl -s -H "Authorization: ApiKey $KEY" -H 'Content-Type: application/json' \
  "$ES/my-index/_search" -d @body.json | jq 'has("aggregations")'
# true

jq '.aggregations=.aggs | del(.aggs)' body.json | elastic es search --index my-index --input-file - | jq 'has("aggregations")'
# true

Misspelt key:

echo '{"qeury":{"term":{"event.action":"no-such-value"}}}' | elastic es count --index my-index --input-file -
# returns the full index count, exit 0
# Elasticsearch directly: 400 "request does not support [qeury]"

Scope observed

Command Unknown top-level key
es search dropped
es count dropped
es esql query dropped
es async-search submit dropped (checked against a local capture server: body keys ["query","size"])
es msearch bodies inside searches pass through, including aggs
kb … forwarded (src/kb/request-builder.ts)

Nested keys are not affected: sub-aggregations under aggregations keep aggs.

Mechanism

  • src/es/request-builder.ts:163-169 copies only schema-derived arguments (src/lib/json-schema-args.ts:193-206) into a new body. Keys outside the schema never reach it.
  • search.request.json in @elastic/schemas 0.10.0 declares properties.aggregations but no aggs.
  • AJV does not remove the key (src/lib/ajv-validate.ts sets no removeAdditional), so --no-validate makes no difference.

Conflicts with AGENTS.md

The input rules in AGENTS.md ("Command Authoring Requirements" → "Input") say:

Reject unknown keys: Input with undefined keys MUST produce a validation error naming the unknown field(s). Silent stripping is not acceptable.

For ES commands, an unknown top-level key is neither rejected nor sent. Applied as written, that rule would make aggs a validation error, which still breaks the most common Elasticsearch body shape. Accepting aggs as an alias of aggregations, and rejecting other unknown keys, satisfies both.

Root cause

Two layers, both needed:

  1. @elastic/schemas doesn't carry the spec's alias. The specification marks aggregations with @aliases aggs (SearchRequest.ts L414-L419). The JSON Schema output drops it, so aggs reaches the CLI as an unknown key. Reported upstream: elastic/schemas-js#125.
  2. The ES request builder drops unknown keys instead of rejecting them (see Mechanism). This is a CLI defect independent of the schemas: a misspelt query returns unfiltered results with exit 0.

If the upstream fix emits aggs as a declared property, the CLI picks it up with no change. If it adds an annotation instead, the CLI must read that annotation. Fixing (2) turns any remaining unknown key into a validation error, as AGENTS.md requires.

Expected

Either option would avoid wrong results with exit 0:

  1. Forward unknown top-level body keys, as the Kibana builder does, and let Elasticsearch accept or reject them. At minimum, accept aggs as an alias of aggregations.
  2. Or fail (or warn on stderr) when --input-file contains a top-level key the command will not send.

Related

  • elastic/cli#170 and elastic/cli#223: rejection of unknown fields, fixed by moving to passthrough schemas. This report is the opposite symptom: no rejection, the key is silently omitted.
  • elastic/cli#699: malformed JSON passing validation. It mentions aggs, but not this omission.
  • elastic/cli#786: request-routing unification. It keeps service-specific behaviour, so it doesn't appear to change this.
主要语言
TypeScript
星标
53
派生
28
平均合并
22 小时 48 分钟
30 天内合并 PR
48

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

elastic/cli 的其他 Issue

查看 elastic/cli 的全部 Issue

相似的 Issue

更多 TypeScript Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。