# What should the bucket path be with a top hits

**URL:** <https://discuss.elastic.co/t/what-should-the-bucket-path-be-with-a-top-hits/329024>\
**Category:** Elasticsearch\
**Created:** [March 31, 2023, 8:16am UTC](https://discuss.elastic.co/t/what-should-the-bucket-path-be-with-a-top-hits/329024 "2023-03-31T08:16:17Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![m.a.tanaka](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@m.a.tanaka](https://discuss.elastic.co/u/m.a.tanaka)\
**Post date:** [March 31, 2023, 8:16am UTC](https://discuss.elastic.co/t/what-should-the-bucket-path-be-with-a-top-hits/329024/1 "2023-03-31T08:16:17Z")

</div>

I am trying to create a query in elasticsearch, which is able to retrieve the documents for each group, which is the latest document within each group and meet a specific criteria. But I have not been able to solve this one.

Say the following documents are indexed in myindex in elasticsearch:

```auto
POST /myindex/_bulk
{ "index":{} }
{ "objid": 1, "ident":"group1","version":1, "chdate": 1, "field1" : 1}
{ "index":{} }
{ "objid": 2, "ident":"group1","version":2, "chdate": 2, "field1" : 0}
{ "index":{} }
{ "objid": 3, "ident":"group1","version":2, "chdate": 3, "field1" : 1}
{ "index":{} }
{ "objid": 4, "ident":"group1","version":2, "chdate": 4, "field1" : 0}
{ "index":{} }
{ "objid": 5, "ident":"group1","version":3, "chdate": 1, "field1" : 0}

```

I would like to find all documents, which has field1 set to x if the document with the highest chdate, for each ident and version, which has field1 set to x.

In a case where x is 0 then the documents, which has objid 4 and 5 should be returned In a case where x is 1 then the documents, which has objid 1 should be returned

ChatGpt suggested this query:

```auto
{
  "size": 0,
  "aggs": {
    "ident": {
      "terms": {
        "field": "ident"
      },
      "aggs": {
        "version": {
          "terms": {
            "field": "version"
          },
          "aggs": {
            "top_hits_agg": {
              "top_hits": {
                "size": 1,
                "sort": [
                  {
                    "chdate": {
                      "order": "desc"
                    }
                  }
                ]
              }
            },
            "field1_filter": {
              "bucket_selector": {
                "buckets_path": {
                  "hits": "top_hits_agg.hits.hits",
                  "field1": "top_hits_agg.hits.hits._source.field1"
                },
                "script": {
                  "source": "params.field1 == 0"
                }
              }
            }
          }
        }
      }
    }
  }
}

```

But elasticsearch comes out with following error

```auto
{
  "error" : {
    "root_cause" : [
      {
        "type" : "action_request_validation_exception",
        "reason" : "Validation Failed: 1: No aggregation found for path [top_hits_agg.hits.hits._source.field1];"
      }
    ],
    "type" : "action_request_validation_exception",
    "reason" : "Validation Failed: 1: No aggregation found for path [top_hits_agg.hits.hits._source.field1];"
  },
  "status" : 400
}

```

Anyone who knows what the bucket path should be in this case

Thanks in advance

---

<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:** [April 28, 2023, 8:17am UTC](https://discuss.elastic.co/t/what-should-the-bucket-path-be-with-a-top-hits/329024/2 "2023-04-28T08:17:16Z")

</div>

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