Search on clincal events

I’m trying to design a search engine based on clinical events to select patients who meet certain criteria. The difficulty is that I need to add temporal constraints between clinical events. For example, I need to be able to answer questions such as ‘Select all patients who had an allergy 5 days after taking medication’.

I’ve already thought about creating a unique event table. Because there are lots of different kind of events. It can be a numerical measurement or a text.

Each patient can be represented by a series of events on a time axis. I can store all these events in a unique table that looks like this.

{
    patient_id : 342
    start_date: 1752359084
    end_date: 1752359084
    domain: ‘drugs’,
    key: ‘atc’,
    value: paracetamol
},
{
    patient_id: 342
    start_date: 1752359084
    end_date: 1752359084
    domain: ‘biology’,
    key: ‘hemoglobin’,
    value: 18

},
{
    patient_id: 342
    start_date: 1752359084
    end_date: 172359094
    domain: ‘document’,
    key: ‘cardiology_report’,
    value: ‘You patient has an endocarditis with several criteria. He get an allergy ...’ 

}

To filter on events and have time constraints, I’ve imagined a hypothetical DSL that looks like this:

event 1 {
    drugs.ATC = ‘paracetamol
}

event 2 {
    biology.hemoglobin > 10;
}

event 3 {
    document.cardiology_report.match(‘allergy’)
}

# Exemple of temporal constraint on events
event3.end > event2.end 
event2 - event1 > 3 days
event3.start > event2.end


I can create my own DSL with python, that could generate a complex opensearch query. Or generate several queries , aggregate them and extract relevant patient after.

So, I’d like to ask your opinion on my problem and possible solution with opensearch before I jump into anything.