Hibernate Query Optimization

I have a use case where am searching for patients objects using lucene(partially search by identifier, name and person attributes from anywhere) which returns many records that need to be narrowed down(filtered further). I would like to use hibernate to pass more filtering criteria(to the already existing set of patients) for example program attributes and addresses so that I have a reduced set that closely have what I want. For those who have experience on how I can do this without affecting the speed and gains already achieved by lucene, to help me out.

@dkayiwa @mksd @raff @mseaton @darius

1 Like

@ningosi, in order to narrow down results efficiently, you need to put data you want to filter on in Lucene. Similar to how PersonAttribute or PatientIdentifier filters were added. Basically add hibernate search annotations on PersonAddress or PatientProgram. Additional filters can be easily chained to the original queries, see the skipSame feature of LuceneQuery.

These modifications need to be done in openmrs-core. We used to back-port such modifications given the benefits of adding extra data to search index and considerably low memory and cpu cost.

As an alternative you could implement an efficient skipSame method, which accepts Hibernate’s criteria instead of LuceneQuery so you could combine Lucene and a regular DB query. You could then do additional db queries on PatientAddress/PatientProgram and combine them with Lucene results. Anyway I would really recommend to index them with Lucene and use the benefits of Lucene as described above.

Let me know, if you have further questions.

I already have the data as PatientResponse in lucene see here, I added the Hibernate annotations on the respective classes that I envisione to filter with and shown here, so the aim is to narrow down that list by applying PatientAddress/PatientProgram using hibernate, any hints on how I can call those here?

So far you’ve only added PatientAddress and PatientProgram to the Lucene index without any fields. You need to annotate class attributes, which you want to search for with @Field. I cannot advise on modifying the code in Bahmni core as I’m not very familiar with that.