How to access _source of top_hits for buckets_path?

My problem is the same as this one: Buckets_path to access field in top_hits aggregation result but it is closed so I am opening this new one with my context.

GET /_search
{
	"query": {
		"bool": {
			"must": [
				{
					"exists": {
						"field": "lvl.keyword"
					}
				}
			]
		}
	},
	"size": 0,
	"aggs": {
		"group": {
			"terms": {
				"field": "pid.keyword",
				"size": 10000
			},
			"aggs": {
				"last_lvl": {
					"top_hits": {
						"sort": [
							{
								"timestamp": {
									"order": "desc"
								}
							}
						],
						"_source": [
							"lvl"
						],
						"size": 1
					}
				},
				"max_lvl": {
					"top_hits": {
						"sort": [
							{
								"lvl.keyword": {
									"order": "desc"
								}
							}
						],
						"_source": [
							"lvl"
						],
						"size": 1
					}
				},
				"lvl_compare": {
					"bucket_selector": {
						"buckets_path": {
							"lastLevel": "last_lvl",
							"maxLevel": "max_lvl"
						},
						"script": "params.lastLevel == params.maxLevel"
					}
				}
			}
		}
	}
}

Response

{
  "error": {
    "root_cause": [],
    "type": "search_phase_execution_exception",
    "reason": "",
    "phase": "fetch",
    "grouped": true,
    "failed_shards": [],
    "caused_by": {
      "type": "aggregation_execution_exception",
      "reason": "buckets_path must reference either a number value or a single value numeric metric aggregation, got: org.elasticsearch.search.aggregations.metrics.tophits.InternalTopHits"
    }
  },
  "status": 503
}
3 Likes

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