Filtering
The search API uses the following filtering options to facilitate the search and allow to perform a wide range of consultations.
Several techniques solve the filtering issue when you’re querying over a RESTful interface. For example, you can use standard HTTP parameters to add filtering capabilities to your query. It’s pretty simple but doesn’t cover complex needs. We require a SQL-like approach, with typical operators like AND
, OR
, EQUAL
, NOT EQUAL
, etc. OpenGate allows you to filter your queries by sending a POST
request to a specific URI. In the POST
request, you must send a JSON document with a fashionable DSL structure. It is a command pattern approach in contrast with the entity/collection pattern used in the provisioning API.
Filtering operators
Filtering comparison operator list
eq
: Equals.neq
: Not equals.like
: Regex pattern like.gt
: Greater than.lt
: Lower than.gte
: Greater than or equals.lte
: Lower than or equals.in[]
: Included in a concrete group.nin[]
: Not included in a concrete group.exists
: Exists.within
: Included in anareas.geometry
GeoJson (exclusive for Area search).
Filtering logical operators list
and[]
: Andor[]
: Or
How to use filters
Let’s suppose we want to filter devices with device.operationalStatus
equals to NORMAL
and with device.communicationModules[].mobile.imei
starting with 351873000102290
.
If we were dealing with a SQL database we’d write the following SQL sentence:
SELECT * FROM device
WHERE device.operationalStatus LIKE 'NORMAL'
AND device.communicationModules[].mobile.imei LIKE '351873000102290'
Note: remember, you can use all the data streams defined in the default data models and your own data streams in the WHERE
clause.
Translating the previous SQL sentence to OpenGate searching API we’ll have:
{
"filter": {
"and": [
{
"like": {
"provision.device.administrativeState": "NORMAL"
}
},
{
// The result will contain all devices with collected operational Status that
// contains NORMAL and are related with communications modules with collected
// imei containing 351873000102290
"like": {
"provision.device.communicationModules[].mobile.imei": "351873000102290"
}
}
]
}
}
The result will contain all devices with collected operational Status that contains NORMAL
and are related to communications modules with collected imei
containing 351873000102290
.
Another example comparing SQL to JSON, searching all devices except the one with serialNumber
equal to 82A75D494B0EBF7A95587285AE78E83F
:
SELECT * FROM device WHERE serialNumber <> '82A75D494B0EBF7A95587285AE78E83F'
/north/v80/search/devices
{
"filter": {
"neq": { "device.serialNumber": "82A75D494B0EBF7A95587285AE78E83F" }
},
"limit": {
"start": 2
}
}
More filtering examples
Click on any of the following links to expand the contents.