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

// 从分组的每个桶取出第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字段排序

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
}

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