查询服务的查询参数
多个 ThingWorx 服务可接受可选查询参数。查询参数为值对象,其配置会根据所请求查询类型的不同而不同。
可能的类型有:
Matches, NotMatches TaggedWith,
NotTaggedWith
GT, LT, GE, LE, NE, EQ, LIKE, NOTLIKE, IN, NOTIN
Between, NotBetween MissingValue,
NotMissingValue Near, NotNear
查询可以为 AND/OR,并且可以包含“排序”。
查询参数可用于以 query 开头的所有服务,其中包括:
所有事物
QueryThingStreamEntries
所有流
QueryStreamData
QueryStreamEntries
QueryStreamEntriesWithData
所有数据表
QueryDataTableEntries
InfoTableFunctions 资源
Query
所有事物模板
QueryImplementingThings
QueryImplementingThingsWithData
所有事物形态
QueryImplementingThings
QueryImplementingThingsWithData
配置
查询参数具有两个可配置选项:filters 和 sorters。定义查询参数后,可以将其传递给查询服务。以下是查询参数的 filter 和 sort 选项的示例。建议您在脚本配置器中使用编写脚本工具和片段作为起点,然后根据需要手动创建查询参数对象。
单一筛选器选项
Matches 或 NotMatches 筛选器
var query =
{
"filters": {
"type": "Matches|NotMatches",
"fieldName": "Source",
"expression": "(Kettle)|(Filler)"
}
};
标记或未标记筛选器
var query = {
"fieldName": "tags",
"type": "NOTTAGGED",
"tags": [
{
"vocabulary": "Applications",
"vocabularyTerm": "Testing"
},
{
"vocabulary": "Plants",
"vocabularyTerm": "Sedona"
}
]
};
单一比较运算符
* 
使用 LIKE/NOTLIKE 时需要添加您自己的通配符 (%、* 或 ? 表示单一字符通配符)。必须使用双反斜杠对特殊字符进行转义,以针对其文本值作出解释。例如,要查找 ThingWorx,请使用 LIKE Th%
var query = {
"filters":{
"type": "GT, LT, GE, LE, NE, EQ, LIKE, NOTLIKE",
"fieldName": "<field_name>",
"value": "Th\\+*"
}
};
In 或 Not In 筛选器
var jsonArray = [12,14];
var query = {
"filters": {
"type": "IN, NOTIN",
"fieldName": "Duration",
"values": jsonArray
}
};
Between 或 NotBetween 筛选器
var query =
{
"filters": {
"type": "Between, NotBetween",
"fieldName": "Duration",
"from": "2",
"to": "12"
}
};
MissingValue 或 NotMissingValue 筛选器
var query =
{
"filters": {
"type": "MissingValue, NotMissingValue",
"fieldName": "OrderQuantity"
}
};
Near 或 NotNear 筛选器
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"
}
}
};
复合筛选器选项
可以使用 AndOr 筛选器类型来组合多个筛选器。下面的示例将筛选持续时间大于 12 且在标记字段中标记为断电维护问题的行。
var query =
{
"filters": {
"type": "And",
"filters": [
{
"type": "GT",
"fieldName": "Duration",
"value": "12"
},
{
"type": "TaggedWith",
"fieldName": "tags",
"tags": "MaintenanceIssues:PowerOutage"
}
]
}
};
具有不同 And/Or 类型的嵌套筛选器
为了嵌套具有不同 And/Or 类型的筛选器,JSON 对象的每个级别都需要 type 和 filters 关键字。下列用于查询的 JSON 语法可获得预期结果 (而不是空字符串,以下示例中的空字符串已经替换为 empty)。
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 }
]
}
};
排序选项
Sorters 选项是一个对象数组。Sorters 数组中的每个排序对象有三个可能的参数:fieldName、isAscending 和 isCaseSensitive。仅字段名称为必填项,而 isAscending 和 isCaseSensitive 默认为 true。
单一排序选项
var query =
{
"sorts": [
{
"fieldName": "Material"
}
]
}
复杂排序选项
var query =
{
"sorts": [
{
"fieldName": "Material"
},
{
"fieldName": "OrderDate",
"isAscending": false
}
]
}
排序和筛选器
您可以将 sort 和 filter 应用到相同的查询服务,如下面的示例所示:
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"
}
]
}
};
可能的类型
可能的类型有:
EQ = 等于
NE = 不等于
GT = 大于
GE = 大于或等于
LT = 小于
LE = 小于或等于
LIKE = 类似
BETWEEN
NOTBETWEEN