# 分桶之后怎么按数据字段排序

**URL:** <https://discuss.elastic.co/t/topic/138109>\
**Category:** 中文提问与讨论\
**Created:** [July 2, 2018, 1:29am UTC](https://discuss.elastic.co/t/topic/138109 "2018-07-02T01:29:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![hubiao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubiao/32/26178_2.png) [@hubiao](https://discuss.elastic.co/u/hubiao)\
**Post date:** [July 2, 2018, 1:29am UTC](https://discuss.elastic.co/t/topic/138109/1 "2018-07-02T01:29:36Z")

</div>

```
// 从分组的每个桶取出第1个
TopHitsAggregationBuilder topAggr = AggregationBuilders.topHits("top")
    .sort("reprint", SortOrder.DESC).sort("pubdate", SortOrder.DESC).size(1);

// 分桶
AggregationBuilder aggregationBuilder = AggregationBuilders.terms("group").field("dataSource")
    .size(8).minDocCount(1).subAggregation(topAggr);

```

我现在代码思路是。按dataSource分桶，然后每个桶里边按 reprint，pubdate排序，取第1个。

**现在怎么根据每个桶取出来的第1个，进行排序，按桶里边字段pubdate字段排序**

---

<div class="post-metadata">

**Author:** ![hubiao](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/hubiao/32/26178_2.png) [@hubiao](https://discuss.elastic.co/u/hubiao)\
**Post date:** [July 2, 2018, 2:03am UTC](https://discuss.elastic.co/t/topic/138109/2 "2018-07-02T02:03:46Z")

</div>

java api还原DSL语法如下，我手动改了下面红色标识的地方

```
GET /xw/_search
{
  "from" : 0,
  "size" : 0,
  "query" : {
    "bool" : {
      "must" : [
        {
          "term" : {
            "stage" : {
              "value" : "1",
              "boost" : 1.0
            }
          }
        },
        {
          "bool" : {
            "should" : [
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "title_ngram_sore" : {
                          "value" : "北京",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "text_nlp" : {
                          "value" : "北京",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "title_ngram_sore" : {
                          "value" : "人工智能",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              },
              {
                "bool" : {
                  "must" : [
                    {
                      "term" : {
                        "text_nlp" : {
                          "value" : "人工智能",
                          "boost" : 1.0
                        }
                      }
                    }
                  ],
                  "disable_coord" : false,
                  "adjust_pure_negative" : true,
                  "boost" : 1.0
                }
              }
            ],
            "disable_coord" : false,
            "adjust_pure_negative" : true,
            "boost" : 1.0
          }
        }
      ],
      "disable_coord" : false,
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "aggregations" : {
    "group" : {
      "terms" : {
        "field" : "dataSource",
        "size" : 8,
        "min_doc_count" : 1,

        "order" : [
          {
            " **_top.pubdate_**" : "desc"
          },
          {
            "_term" : "asc"
          }
        ]
      },
      "aggregations" : {
        "top" : {
          "top_hits" : {
           
            "from" : 0,
            "size" : 1,
            "version" : false,
            "explain" : false,
            "sort" : [
              {
                "reprint" : {
                  "order" : "desc"
                }
              },
              {
                "pubdate" : {
                  "order" : "desc"
                }
              }
            ]
          }
        }
      }
    }
  }
}

```

执行报如下错误

```
{
   "error": {
      "root_cause": [
         {
            "type": "aggregation_execution_exception",
            "reason": "Invalid terms aggregation order path [top.pubdate]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "bz",
            "node": "9GXVKVesQXCKY4vheQqtpQ",
            "reason": {
               "type": "aggregation_execution_exception",
               "reason": "Invalid terms aggregation order path [top.pubdate]. Terms buckets can only be sorted on a sub-aggregator path that is built out of zero or more single-bucket aggregations within the path and a final single-bucket or a metrics aggregation at the path end."
            }
         }
      ]
   },
   "status": 500
}
```

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 30, 2018, 2:03am UTC](https://discuss.elastic.co/t/topic/138109/3 "2018-07-30T02:03:46Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
