# How to access \_source of top\_hits for buckets\_path?

**URL:** <https://discuss.elastic.co/t/how-to-access-source-of-top-hits-for-buckets-path/167743>\
**Category:** Elasticsearch\
**Created:** [February 10, 2019, 9:03am UTC](https://discuss.elastic.co/t/how-to-access-source-of-top-hits-for-buckets-path/167743 "2019-02-10T09:03:49Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShawnClark](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/shawnclark/32/40566_2.png) [@ShawnClark](https://discuss.elastic.co/u/ShawnClark)\
**Post date:** [February 10, 2019, 9:03am UTC](https://discuss.elastic.co/t/how-to-access-source-of-top-hits-for-buckets-path/167743/1 "2019-02-10T09:03:49Z")

</div>

My problem is the same as this one: [Buckets\_path to access field in top\_hits aggregation result](https://discuss.elastic.co/t/buckets-path-to-access-field-in-top-hits-aggregation-result/88093) 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
}
```

---

<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:** [March 10, 2019, 9:03am UTC](https://discuss.elastic.co/t/how-to-access-source-of-top-hits-for-buckets-path/167743/2 "2019-03-10T09:03:55Z")

</div>

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