Stream: implementers
Topic: Search _has not
Brett Esler (Nov 02 2018 at 12:53):
I was looking for a way to make a _has NOT search
/Patient?_has:Condition:subject:code=1234 - gives me patients with a condition by code; how would I search for patients who don't have a specific condition?
Alexander Zautke (Nov 02 2018 at 14:43):
Because you are eventually searching on Condition.code, which is a token search parameter, you can use the :not modifier.
[base]/Patient?_has:Condition:subject:code:not=1234
This would give you all patients that have a condition with a code, which differs from 1234.
You can also use :missing=true, which gives you all patients where the linked condition does not contain a code.
Brett Esler (Nov 04 2018 at 10:20):
I thought this also - but wouldn't this return all patients that have a least one condition other than 1234 which would not include patients that do not have any Conditions recorded?
Alexander Zautke (Nov 04 2018 at 12:51):
Yes, that's true. So, basically, you would need to execute two queries: a) :code:not and b) :missing=false. The union of the two result sets then contains all records that do have a code, but which are different AND all records which do not have a code.
Theoretically, you can also use _filter for it, though, not many servers support this kind of request.
With _filter you can do an OR query, which the "regular" search API does not allow:
[base]/Patient?_filter=_has:Condition:subject:code ne 1234 or _has:Condition:subject:missing eq false
Edit: While writing the _filter statement above, I was running into the issue that the ':' character is rejected by the _filter grammar. See this thread. Sorry, for that.
Brett Esler (Nov 12 2018 at 12:50):
thanks @Alexander Zautke
Last updated: Apr 12 2022 at 19:14 UTC