--- title: "遇到多个排序值 – 如何解决此 Elasticsearch 异常" date: 2026-03-24 lastmod: 2026-03-24 description: "当 Elasticsearch 尝试对包含多个值的字段进行排序时,会发生此错误。本文介绍了解决方案,包括使用脚本合并值、使用 max/min 函数等方法。" tags: ["排序异常", "聚合查询", "多值字段", "GeoLine"] summary: " 版本: 7.11-7.15 简要来说,当 Elasticsearch 尝试对具有多个值的字段进行排序时,会发生此错误。Elasticsearch 只能对单值字段进行排序。要解决此问题,您可以更改数据模型以确保用于排序的字段在每个文档中只有一个值,或者使用脚本将多个字段值合并为一个值用于排序。另外,您可以使用 max 或 min 函数从多个值中选择一个值进行排序。 日志上下文 # 日志 “Encountered more than one sort value for a” 的类名是 GeoLineBucketedSort.java。我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入了解的人参考: @Override protected boolean advanceExact(int doc) throws IOException { if (docSortValues.advanceExact(doc)) { if (docSortValues.docValueCount() > 1) { throw new AggregationExecutionException("Encountered more than one sort value for a " + "single document. Use a script to combine multiple sort-values-per-doc into a single value."); } // There should always be one weight if advanceExact lands us here; either // a real weight or a `missing` weight " --- > **版本:** 7.11-7.15 简要来说,当 Elasticsearch 尝试对具有多个值的字段进行排序时,会发生此错误。Elasticsearch 只能对单值字段进行排序。要解决此问题,您可以更改数据模型以确保用于排序的字段在每个文档中只有一个值,或者使用脚本将多个字段值合并为一个值用于排序。另外,您可以使用 max 或 min 函数从多个值中选择一个值进行排序。 日志上下文 ----------- 日志 "Encountered more than one sort value for a" 的类名是 [GeoLineBucketedSort.java](https://www.geeksforgeeks.org/java-lang-class-class-java-set-1/)。我们从 Elasticsearch 源代码中提取了以下内容,供那些寻求深入了解的人参考: ```java @Override protected boolean advanceExact(int doc) throws IOException { if (docSortValues.advanceExact(doc)) { if (docSortValues.docValueCount() > 1) { throw new AggregationExecutionException("Encountered more than one sort value for a " + "single document. Use a script to combine multiple sort-values-per-doc into a single value."); } // There should always be one weight if advanceExact lands us here; either // a real weight or a `missing` weight ```