--- title: "Max children 仅支持嵌套排序的顶层 - 如何解决此 Elasticsearch 异常" date: 2026-03-19 lastmod: 2026-03-19 description: "当在 Elasticsearch 的非顶层嵌套排序中使用 'max_children' 参数时会出现此错误。该参数仅允许在嵌套排序的顶层使用。" tags: ["嵌套排序", "异常处理", "查询优化"] summary: " 版本: 7.16-8.9 简而言之,当在 Elasticsearch 的非顶层嵌套排序中使用 ‘max_children’ 参数时会出现此错误。‘max_children’ 参数仅允许在嵌套排序的顶层使用。要解决此问题,你应该确保 ‘max_children’ 仅在嵌套排序的顶层使用。如果它在更深层级中使用,你应该重构查询以符合此要求。或者,如果可行且能简化查询,可以考虑重组数据结构。 日志上下文 # 日志 “max_children is only supported on top level of nested sort” 的类名是 FieldSortBuilder.java。 我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入上下文的人参考: * 如果 max children 不位于顶层嵌套排序,则抛出异常。 */ static void validateMaxChildrenExistOnlyInTopLevelNestedSort(SearchExecutionContext context; NestedSortBuilder nestedSort) { for (NestedSortBuilder child = nestedSort.getNestedSort(); child != null; child = child.getNestedSort()) { if (child.getMaxChildren() != Integer.MAX_VALUE) { throw new QueryShardException(context; "max_children is only supported on top level of nested sort"); } } } /** " --- > **版本:** 7.16-8.9 简而言之,当在 Elasticsearch 的非顶层嵌套排序中使用 'max_children' 参数时会出现此错误。'max_children' 参数仅允许在嵌套排序的顶层使用。要解决此问题,你应该确保 'max_children' 仅在嵌套排序的顶层使用。如果它在更深层级中使用,你应该重构查询以符合此要求。或者,如果可行且能简化查询,可以考虑重组数据结构。 日志上下文 ------- 日志 "max\_children is only supported on top level of nested sort" 的类名是 [FieldSortBuilder.java。](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/) 我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入上下文的人参考: ```java * 如果 max children 不位于顶层嵌套排序,则抛出异常。 */ static void validateMaxChildrenExistOnlyInTopLevelNestedSort(SearchExecutionContext context; NestedSortBuilder nestedSort) { for (NestedSortBuilder child = nestedSort.getNestedSort(); child != null; child = child.getNestedSort()) { if (child.getMaxChildren() != Integer.MAX_VALUE) { throw new QueryShardException(context; "max_children is only supported on top level of nested sort"); } } } /** ```