Cohorts and Cohort Searches

thanks. We will follow up on this.

@reddberckley, let me know how things turned out.

Hi @darius. We’ve been able to make searches for Patient attributes and we anticipate being able to do so for most of the other components. We anticipate having an issue with Drug Order as you pointed out earlier. We don’t think the existing cohort definitions would suffice for that. We want to focus on completing the others at the moment but we would appreciate hearing your thoughts on that and any other tab that may be tricky to implement.

We’ve uploaded the Patient attribute search at

Cool! I love to see search results in OpenMRS. :slight_smile:

I happened to notice a small bug, which I’ll mention (though I assume this would be covered by some other story that is already in your backlog):

  1. search for age between 0 and 50
  2. click “Next” 5 times
  3. now search for age between 0 and 1
  4. You won’t see any results, and the paging display will say “6 of 4”

In general, I hope that when someone submits a search: (a) it adds a row to the Search History table, (b) it clears the search form that you submitted.

1 Like

Hello @darius, thank you for spotting this. We’ll have to create a new issue to fix that interesting bug. :smile:
Also, we’ll look into how to populate the search history table and clearing of the form fields. However, we’re currently looking at performing as many searches as possible.

Thank you once again.

Hi @darius the link for the wiki ( is no longer accessible. It just redirects to the repo. I don’t know what’s wrong. Thanks

Someone had turned the wiki off in the project settings. I don’t know who or why.

I have turned it back on, and the documentation is available again.


Hi @darius sequel to the demo call we had, I’ve made a list of all the searches we’ve been able to do and those that we’ve not been able to do

Searches that we have done Patient Attributes

  • Search patients by gender
  • Search by age (both minimum & maximum age)
  • Search patients by using date of birth
  • Search patients that are dead / alive or both
  • Search using a combination of 2 or more of the above fields
  • Search by using attributes such as citizenship, birth place, civil status, Mother’s name, race etc

Encounter search

  • Search patients based on encounter
  • Search patients with encounter at a particular location
  • Search patients with a minimum number of encounter
  • Search with a maximum number of encounter

Searches that we can’t do

  1. Concept & Observation a. We can’t see a cohort definition that supports searching patients who have (or does not have) a particular observation

  2. Encounter a. There is no parameter that supports fields like (within the last months and days, since and until). We had a workaround as regards this by using date but we don’t know if that will suffice. c. Search by form also generates an error, the link to the error is here The encounterAdvancedSearch definition library key was added here

  3. Programme Enrollment a. No definition library key

  4. Drug Order a. No cohort definition

cc @mseaton

Hi @femi,

There are more options available for these needs:

These certainly exist. You should be able to use NumericObsCohortDefinition, CodedObsCohortDefinition, DateObsCohortDefinition, and TextObsCohortDefinition

This is possible by use of expressions when mapping parameters. There is some explanation of what is available to do this here. It is also possible by creating an appropriate definition library that takes in the number of months/days and units, and converts this to a date.

Based on the error message I see here:

java.lang.NullPointerException\n\tat\n\tat org.openmrs.module.reporting.cohort.query.db.hibernate.HibernateCohortQueryDAO.getPatientsHavingEncounters(\n\tat org.openmrs.module.reporting.cohort.query.service.CohortQueryServiceImpl.getPatientsHavingEncounters(\n\tat

My best guess is that this is caused more by bad test data than by any problem in the reporting framework. Were you testing this on patients and encounters that were previously saved, or just created in memory and not yet persisted?

I’m not sure what this means, but we should be able to just add in more library methods to meet our needs. There are several program-related cohort definitions available: ProgramEnrollmentCohortDefinition, PatientStateCohortDefinition, InProgramCohortDefinition, InStateCohortDefinition.

This does appear to be missing. We’d need to ticket this and get it added to the reporting framework.

Hope this helps a little. I’m on vacation this week, so I may be slower following up than normal. @darius FYI.


1 Like

By the way, the old cohort builder has a feature to search for something like “patients with any observations for which this concept is the answer”. I think we should get rid of this function. (@ssmusoke please confirm this). Getting rid of it should mean that the existing definitions that Mike lists here are sufficient.

@jeffneiman can we schedule to talk on the next design forum about “what drug order search functionality to support in the Reporting module”

Will do! Shall we say for this Monday?

Hi @mseaton

Thanks for pointing out these observation cohort definitions. We have tried looking at them, however, we are experiencing challenges making patient observation searches with them. When we worked on encounter search, we added a definition library to the built-in cohort definition file in this PR REPORT-801: Add an anyEncounterDuringPeriodWithOccurrence to the BuiltInCohortDefinitionLibrary by fortunee · Pull Request #140 · openmrs/openmrs-module-reporting · GitHub, using that, we were able to compose a JSON of the format shown below for the reportingrest/adhocquery resource:

  "type": "org.openmrs.module.reporting.dataset.definition.PatientDataSetDefinition",
  "columns": [
      "type": "",
      "name": "Given Name",
      "key": "reporting.library.patientDataDefinition.builtIn.preferredName.givenName"
      "type": "",
      "name": "Family Name",
      "key": "reporting.library.patientDataDefinition.builtIn.preferredName.familyName"
  "customRowFilterCombination": "1",
  "rowFilters": [
    	"type": "org.openmrs.module.reporting.cohort.definition.PatientDataDefinition",
    	"key": "reporting.library.cohortDefinition.builtIn.encounterSearchAdvanced",
    	"parameterValues": {
    		"encounterTypes": ["ADULTINITIAL"]

We would like to clarify how we can specify parameterValues for the query as shown above for the observation search form shown below for Hepatotoxicity:


Hi @ethan,

So in this case, just like you did with the encounter search, you would take a similar approach and create a “codedObsSearchAdvanced” definition (or some better name). This would instantiate a new CodedObsCohortDefinition, and expose each of it’s configuration properties. Among these are:

  • “question” - reference to the Hepatotoxicity concept in your example
  • “onOrAfter” - the “since” parameter in your example
  • “onOrBefore” - the “until” parameter in your example

To support the last X months or Y days, it is a bit more complicated. Now that I look at it, probably the easiest thing to do would be to convert these into dates in UI layer, in JS, and then pass these dates through to the “onOrAfter” and “onOrBefore” as if the user entered these as dates directly.

Does this make sense? @darius, any other comments?


Hi @mseaton,

Thanks again for the pointers and clarification, we’ll work in this line and reach out again if we encounter further challenges.

Regards, Ethan

I would just skip these in the first version, and put a ticket in the backlog to add them later.

(@ssmusoke do you know if in practice the “obs in last X months / Y days” is used much in the cohort builder? (E.g. is just having the date range a sufficient MVP?)

@darius I would keep a date range for now, since the in the last X months or Y days still amounts to a date range which is more important the time from today, since the the typical questions are to do what happened in a specific month or quarter.

Hi @mseaton,

Hope you’re doing great.

We’ve taken extensive look at the CodedObsCohortDefinition cohort definition and tried adding a codedObsSearchAdvanced definition library like we discussed but we discovered that CodedObsCohortDefinition does not have question, onOrAfter or onOrBefore configuration properties like we have in the other cohort definitions, instead it has just operator and valueList configuration properties.

We have explored the option of adding these configuration properties (i.e. question, onOrAfter or onOrBefore) to the CodedObsCohortDefinition and specify getters and setters for them and expose them to the new codedObsSearchAdvanced but that too does not solve the problem.

We’re wondering how we can use the already specified configuration properties (i.e. operator and valueList) to make searches based on observations?

We also tried using the DateObsCohortDefinition cohort definition to achieve this to no avail.


@ethan, sorry for the confusion. The CodedObsCohortDefinition does have these configuration properties. They are defined in a superclass - BaseObsCohortDefinition - that the more specific ObsCohortDefinition implementations extend.


Yes @mseaton, I thought about that too, but then I discovered BaseObsCohortDefinition cannot be instantiated given that it is an abstract class.

Is there a way we can make the public sub-classes inherit these configuration properties from BaseObsCohortDefinition?

Also, I came across ObsInEncounterCohortDefinition which I extended to create the definition library and it returned search results, and we were wondering if this is sufficient for performing the observation searches?

Thanks, Ethan

@ethan, the public sub-classes do inherit these configuration properties.