How can I search documents by tags?
It is a common use case to store a list of tags associated with a Document. For instance, you may want to tag a patient with various labels such as High Blood Pressure
, and Spanish-Speaking
.
You may store a Document that looks like the following:
{
"name": "John Doe",
"tags": ["High Blood Pressure", "Spanish-Speaking"]
}
Once stored as an array, the in
filter will perform an OR
search for a list of tags. To get all documents with High Blood Pressure
, you can use the eq
filter with High Blood Pressure
as the value instead of the in
filter which is meant for lists of values.
The following will return all documents with the tag High Blood Pressure
:
{
"filter": {
"tags": {
"type": "eq",
"value": "High Blood Pressure",
"case_sensitive": false
}
}
}
The following will return all documents with either the tags High Blood Pressure
or Spanish-Speaking
or both:
{
"filter": {
"tags": {
"type": "in",
"value": ["High Blood Pressure", "Spanish-Speaking"],
"case_sensitive": false
}
}
}
If you need to find all documents that only contain High Blood Pressure
, the easiest way to accomplish this is to add another field (e.g. tag_count
) that contains the number of tags for the document:
{
"filter": {
"tags": {
"type": "eq",
"value": "High Blood Pressure",
"case_sensitive": false
},
"tag_count": {
"type": "eq",
"value": 1,
}
},
"filter_type": "and"
}