ThingWorx Model Definition and Composer > Things > Thing Services > Query Parameter for Query Services
  
Query Parameter for Query Services
Several ThingWorx services accept an optional query parameter. The query parameter is a values object that is configured differently depending on the type of query being requested.
Possible types are:
Matches, NotMatches TaggedWith,
NotTaggedWith
GT, LT, GE, LE, NE, EQ, LIKE, NOTLIKE, IN, NOTIN
Between, NotBetween MissingValue,
NotMissingValue Near, NotNear
Queries can be AND/OR and can include a Sort.
The query parameter is used in all services that start with Query among them are:
All things
QueryThingStreamEntries
All streams
QueryStreamData
QueryStreamEntries
QueryStreamEntriesWithData
All data tables
QueryDataTableEntries
InfoTableFunctions resource
Query
All thing templates
QueryImplementingThings
QueryImplementingThingsWithData
All thing shapes
QueryImplementingThings
QueryImplementingThingsWithData
Configuration
The query parameter has two configurable options: filters and sorters. After defining the query parameter, it can be passed into a query service. Below are examples of the filters and sorters options for the query parameter. It is recommended that you use the scripting tools and snippets in the script configurator as a starting point and then manually create the query parameter object as appropriate.
Single Filter Options
Matches or NotMatches filter
var query =
{
"filters": {
"type": "Matches|NotMatches",
"fieldName": "Source",
"expression": "(Kettle)|(Filler)"
}
};
TAGGED or NOTTAGGED filter
var query = {
"fieldName": "tags",
"type": "NOTTAGGED",
"tags": [
{
"vocabulary": "Applications",
"vocabularyTerm": "Testing"
},
{
"vocabulary": "Plants",
"vocabularyTerm": "Sedona"
}
]
};
Single Comparator
* 
When using LIKE/NOTLIKE you need to add your own Wild Cards (% or * or ? for a single character wild card). For example to find ThingWorx use LIKE Th%
var query =
{
"filters": {
"type": "GT, LT, GE, LE, NE, EQ, LIKE, NOTLIKE",
"fieldName": "Duration",
"value": "12"
}
};
In or Not In filter
var jsonArray = [12,14];
var query = {
"filters": {
"type": "IN, NOTIN",
"fieldName": "Duration",
"values": jsonArray
}
};
Between or NotBetween filter
var query =
{
"filters": {
"type": "Between, NotBetween",
"fieldName": "Duration",
"from": "2",
"to": "12"
}
};
MissingValue or NotMissingValue filter
var query =
{
"filters": {
"type": "MissingValue, NotMissingValue",
"fieldName": "OrderQuantity"
}
};
Near or NotNear filter
var query =
{
"filters": {
"type": "Near, NotNear",
"fieldName": "fieldName",
"distance": "50",
"units": "M(iles), K(ilometers), N(autical miles)",
"location": {
"latitude": "40.12",
"longitude": "51.24",
"elevation": "300",
"units": "WGS84"
}
}
};
Composite Filter Options
It is possible to combine multiple filters using either the And or Or filter type. The example below will filter for rows that have a duration greater than 12 and are tagged with a power outage maintenance issue in the tags field.
var query =
{
"filters": {
"type": "And",
"filters": [
{
"type": "GT",
"fieldName": "Duration",
"value": "12"
},
{
"type": "TaggedWith",
"fieldName": "tags",
"tags": "MaintenanceIssues:PowerOutage"
}
]
}
};
Nesting Filters with Different And/Or Types
In order to nest filters with different And/Ortypes, there must be a type and filters keyword for each level of the JSON Object. The following JSON syntax for the query will obtain the desired results (other than the empty string, which is replaced with empty in the example below).
var query3 = {
"filters": {
"type": "AND",
"filters": [
{
"type": "OR",
"filters": [
{ "fieldName": "Status", "type": "LIKE", "value": "*-none-*" }
,
{ "fieldName": "Status", "type": "LIKE", "value": "empty" }
]
},
{ "fieldName": "IsDeleted", "type": "EQ", "value": false }
]
}
};
Sorting Options
The sorters option is an object array. There are three possible parameters for each of the sort objects in the sorters array fieldName, isAscending, isCaseSensitive. Only field name is required. isAscending and isCaseSensitive default to true.
Single Sort Option
var query =
{
"sorts": [
{
"fieldName": "Material"
}
]
}
Complex Sort Option
var query =
{
"sorts": [
{
"fieldName": "Material"
},
{
"fieldName": "OrderDate",
"isAscending": false
}
]
}
Sort and Filter
It is possible to apply a sort and a filter to the same query service. An example is show below.
var query =
{
"sorts": [
{
"fieldName": "LotID",
"isAscending": false,
"isCaseSensitive": false
},
{
"fieldName": "Plant"
}
],
"filters": {
"type": "And",
"filters": [
{
"type": "GT",
"fieldName": "Duration",
"value": "12"
},
{
"type": "TaggedWith",
"fieldName": "tags",
"tags": "MaintenanceIssues:PowerOutage"
}
]
}
};
Possible Types
Possible types are:
EQ = equal to
NE = not equal to
GT = greater than
GE = greater than or equal to
LT = less than
LE = less than or equal to
LIKE = like
BETWEEN
NOTBETWEEN