beda-software / fhir-py

FHIR Client for python
MIT License
168 stars 31 forks source link

Example re: paging through a FHIR bundle? #84

Closed cbarina closed 2 years ago

cbarina commented 2 years ago

I want to summarize what codes are in a certain dataset (loinc codes), on DocumentReference.

I am trying to understand how one could iterate through a search result. I understand .limit(x) gives you the first x results, but then if you have a count >100, how do you go to the next page/set of results?

I have also tried fetch_all(), but there are about 1M records I am pulling so it isn't ideal.

mkizesov commented 2 years ago

You can iterate through dataset using lazy fetch with async for on searchset like this:

async for document_reference in client.resources('DocumentReference').sort('-date').limit(100):
        print(document_reference.id)

limit() is an example of a bad naming here :) it means a page size – _count in FHIR https://github.com/beda-software/fhir-py/blob/8fe853fd3340692d5cef82919b7baa42c5b5468b/fhirpy/base/searchset.py#L296

So if you fetch only first page, it works like limit. If you fetch all pages/or one by one, it means just a page size.

If you use SyncFHIRClient instead of AsyncFHIRClient, it works the same with for loop and fetches a new page when the end of current page is reached in a loop.