# Can you help me see how my code should be implemented?

**URL:** <https://discuss.elastic.co/t/can-you-help-me-see-how-my-code-should-be-implemented/351330>\
**Category:** Elasticsearch\
**Tags:** curator\
**Created:** [January 18, 2024, 9:12am UTC](https://discuss.elastic.co/t/can-you-help-me-see-how-my-code-should-be-implemented/351330 "2024-01-18T09:12:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ivanz](https://avatars.discourse-cdn.com/v4/letter/i/a3d4f5/32.png) [@ivanz](https://discuss.elastic.co/u/ivanz)\
**Post date:** [January 18, 2024, 9:12am UTC](https://discuss.elastic.co/t/can-you-help-me-see-how-my-code-should-be-implemented/351330/1 "2024-01-18T09:12:56Z")

</div>

Now there is a requirement as follows:  
A piece of data has four fields: article\_id, year, title, and version. If multiple articles share an article\_id, the one with the highest version in the hit data will be returned during the search. At the same time, the returned aggregated data needs to be paginated. Display | Then the years in the displayed list need to be aggregated and grouped twice, and because there is a lot of data, paging and search functions are needed after aggregation and grouping. How should this be implemented? Now the first step of the code is already there:  
First, aggregate article\_id, then use top\_hits to get the largest one, and use buckert\_sort to implement paging capabilities. How should we improve the remaining secondary aggregation and paging and search of bucket year?

```auto
GET /ar_test2/_search
{
  "size": 0,
  "aggs": {
    "group_by_article_id": {
      "terms": {
        "field": "article_id"
      },
      "aggs": {
        "latest_documents": {
          "top_hits": {
            "_source": ["*"],
            "sort": [{ "version": "desc" }],
            "size": 1
          }
        },
        "group_by_year": {
          "terms": {
            "field": "year",
            "size": 10,
            "order": {
              "_key": "desc"
            }
          }
        },
        "max_version": {
          "max": {
            "field": "version"
          }
        },
        "bucket_sorter": {
          "bucket_sort": {
            "sort": [
              {
                "max_version": {
                  "order": "desc"
                }
              }
            ],
            "from": 0,
            "size": 5
          }
        }
      }
    }
  }
}

```

---

<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:** [February 15, 2024, 9:13am UTC](https://discuss.elastic.co/t/can-you-help-me-see-how-my-code-should-be-implemented/351330/2 "2024-02-15T09:13:56Z")

</div>

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