# Total Record/count FHIR endpoint / List data pagination implementation approach

**URL:** <https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651>\
**Category:** FHIR\
**Tags:** fhir, ohri\
**Created:** [May 20, 2022, 4:34am UTC](https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651 "2022-05-20T04:34:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![amugume](https://talk.openmrs.org/letter_avatar/amugume/32/6_449c533f586939b6e5f246632a46421f.png) [@amugume](https://talk.openmrs.org/u/amugume)\
**Post date:** [May 20, 2022, 4:34am UTC](https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651/1 "2022-05-20T04:34:37Z")

</div>

Team,

We have a requirement to paginate list data in the OHRI widgets / tables that display it. We are experimenting an implementation approach but this requires that we know the total count/number of records before hand in order to paginate. Looked around the FHIR documentation but can’t seem to see any endpoint that returns the total count of say the Encounter Resource given certain filter criteria.

Do we have any such endpoint? Secondly, any ideas on implementation approaches to paginate list data in the micro-frontends?

@samuel34 @ssmusoke @alaboso @eudson @vineeth68@dkayiwa @ibacher

---

<div class="post-metadata">

**Author:** ![burke](https://talk.openmrs.org/user_avatar/talk.openmrs.org/burke/32/17_2.png) [@burke](https://talk.openmrs.org/u/burke)\
**Post date:** [May 20, 2022, 4:57am UTC](https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651/2 "2022-05-20T04:57:23Z")

</div>

> [@amugume](#):
>
> total count of say the Encounter Resource given certain filter criteria

Is it fair to assume filter criteria are simple and are indexed in the database. Returning a total count for arbitrary criteria, complex filters, or criteria that include data that aren’t indexed would not perform well in existing systems that have hundreds of thousands or millions of encounters.

---

<div class="post-metadata">

**Author:** ![corneliouzbett](https://talk.openmrs.org/user_avatar/talk.openmrs.org/corneliouzbett/32/15464_2.png) [@corneliouzbett](https://talk.openmrs.org/u/corneliouzbett)\
**Post date:** [May 20, 2022, 7:57am UTC](https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651/3 "2022-05-20T07:57:43Z")

</div>

> [@amugume](#):
>
> Do we have any such endpoint? Secondly, any ideas on implementation approaches to paginate list data in the micro-frontends?

Yes, all FHIR search endpoints return paginated list data. Probably, we haven’t done a good job documenting. Get more insights from the initial implementation [here](https://github.com/openmrs/openmrs-module-fhir2/commit/1bf7948faf4907937387835e091b4a40018a8654).

Things(GPs configuration) to take note of:

1. Paging default - Sets the default page size
2. Paging maximum - Sets the maximum page size

For example; A paginated list of vital data. From the first request, you will be able to know the total size of resources, and links (`next` & `prev`)

```auto
/ws/fhir2/R4/Observation?subject:Patient=f4d2d9e4-b6a4-426e-b6be-92a9b61faf8a&code=5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,5089AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA,1343AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&_summary=data&_sort=-date

```

Response;

```auto
{
   "resourceType":"Bundle",
   "id":"42416a13-e241-4f7e-ac4d-34853c5e4310",
   "meta":{
      "lastUpdated":"2022-05-20T07:45:13.145+00:00",
      "tag":[
         {
            "system":"http://terminology.hl7.org/CodeSystem/v3-ObservationValue",
            "code":"SUBSETTED",
            "display":"Resource encoded in summary mode"
         }
      ]
   },
   "type":"searchset",
   "total":44,
   "link":[
      {
         "relation":"self",
         "url":"http://openmrs:8080/openmrs/ws/fhir2/R4/Observation?_sort=-date&_summary=data&code=5090AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2C5089AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA%2C1343AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA&subject%3APatient=f4d2d9e4-b6a4-426e-b6be-92a9b61faf8a"
      },
      {
         "relation":"next",
         "url":"http://openmrs:8080/openmrs/ws/fhir2/R4?_getpages=2d1df6b7-f676-4551-95a5-f7ec3093ab99&_getpagesoffset=10&_count=10&_bundletype=searchset"
      }
   ],
   "entry":[
      //Omitted entries
   ]
}

```

Most of the microfrontend widgets don’t utilize backend paging.

---

<div class="post-metadata">

**Author:** ![ibacher](https://talk.openmrs.org/user_avatar/talk.openmrs.org/ibacher/32/19751_2.png) [@ibacher](https://talk.openmrs.org/u/ibacher)\
**Post date:** [May 20, 2022, 1:09pm UTC](https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651/4 "2022-05-20T13:09:04Z")

</div>

> [@amugume](#):
>
> Looked around the FHIR documentation but can’t seem to see any endpoint that returns the total count of say the Encounter Resource given certain filter criteria.

As a general principle, we haven’t documented things where we’re just conforming to the FHIR spec. As @corneliouzbett pointed out, there is a `total` element in all search bundles. While not strictly required by the spec, it is returned for all searches for OpenMRS. If you want to know _just_ the total, you can run the query with `?_summary=count` as [noted in the FHIR search document](https://www.hl7.org/fhir/search.html#summary).

By default, the FHIR2 API returns 10 results (this can be changed via the `fhir2.paging.default`). You can request more resources by using the `_count=...` search parameter (also documented in the [FHIR search documentation](https://www.hl7.org/fhir/search.html#count)) up to the value of the `fhir2.paging.maximum` global property (which defaults to 100).

> [@burke](#):
>
> Returning a total count for arbitrary criteria, complex filters, or criteria that include data that aren’t indexed would not perform well in existing systems that have hundreds of thousands or millions of encounters.

This is a very important consideration and the FHIR2 search API can easily be abused to create a pathological query.

> [@corneliouzbett](#):
>
> Most of the microfrontend widgets don’t utilize backend paging.

Strictly speaking, most frontend widgets are unaware that they are only using paged results and don’t properly handle things 🙂.

---

<div class="post-metadata">

**Author:** ![amugume](https://talk.openmrs.org/letter_avatar/amugume/32/6_449c533f586939b6e5f246632a46421f.png) [@amugume](https://talk.openmrs.org/u/amugume)\
**Post date:** [May 23, 2022, 7:43am UTC](https://talk.openmrs.org/t/total-record-count-fhir-endpoint-list-data-pagination-implementation-approach/36651/5 "2022-05-23T07:43:05Z")

</div>

@ibacher @corneliouzbett noted with thanks
