--- title: "Terms 聚合中参数 required_size 和 shard_size 必须大于 0 – 如何解决此 Elasticsearch 异常" date: 2026-03-26 lastmod: 2026-03-26 description: "在 Elasticsearch 查询的 terms 聚合中,当 'required_size' 和 'shard_size' 参数设置为 0 或更小时会出现此错误。这些参数分别控制返回的唯一词条数量和每个分片考虑的唯一词条数量。要解决此问题,请确保这两个参数都设置为正整数。如果您不想限制词条数量,可以将这些参数设置为一个非常大的数。" tags: ["Terms聚合", "查询参数", "异常处理"] summary: "版本: 6.8-8.9 简而言之,当 Elasticsearch 查询的 terms 聚合中的 ‘required_size’ 和 ‘shard_size’ 参数设置为 0 或更小时,会出现此错误。这些参数分别控制返回的唯一术语数量和每个分片要考虑的唯一术语数量。要解决此问题,请确保将这两个参数都设置为正整数。如果不想限制术语数量,可以将这些参数设置为一个非常大的数。 日志上下文 # 日志 “parameters [required_size] and [shard_size] must be >0 in terms aggregation.” 的类名是 TermsAggregator.java. 我们从 Elasticsearch 源代码中提取了以下内容,供那些需要深入了解上下文的人参考: if (shardMinDocCount > minDocCount) { setShardMinDocCount(minDocCount); } if (requiredSize <= 0 || shardSize <= 0) { throw new ElasticsearchException("parameters [required_size] and [shard_size] must be >0 in terms aggregation."); } if (minDocCount < 0 || shardMinDocCount < 0) { throw new ElasticsearchException("parameter [min_doc_count] and [shardMinDocCount] must be >=0 in terms aggregation." --- > **版本:** 6.8-8.9 简而言之,当 Elasticsearch 查询的 terms 聚合中的 'required_size' 和 'shard_size' 参数设置为 0 或更小时,会出现此错误。这些参数分别控制返回的唯一术语数量和每个分片要考虑的唯一术语数量。要解决此问题,请确保将这两个参数都设置为正整数。如果不想限制术语数量,可以将这些参数设置为一个非常大的数。 日志上下文 ----------- 日志 "parameters [required\_size] and [shard\_size] must be >0 in terms aggregation." 的类名是 [TermsAggregator.java.](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/) 我们从 Elasticsearch 源代码中提取了以下内容,供那些需要深入了解上下文的人参考: ```java if (shardMinDocCount > minDocCount) { setShardMinDocCount(minDocCount); } if (requiredSize <= 0 || shardSize <= 0) { throw new ElasticsearchException("parameters [required_size] and [shard_size] must be >0 in terms aggregation."); } if (minDocCount < 0 || shardMinDocCount < 0) { throw new ElasticsearchException("parameter [min_doc_count] and [shardMinDocCount] must be >=0 in terms aggregation."); } ```