Smart Query

The Smart Query Stage is used to read the Namespace. The stage queries all or part of a namespace, filters, and transforms the results to make pipeline processing easy. The stage uses JSONata syntax to query the namespace.

smart_query_stage.png

From

Controls what part of the namespace is queried. This is similar to a SQL FROM clause, but using JSONata syntax. Below are some examples.

From Query Description
Boston.Paint.Line1.* This is the configuration Namespace, and gets Line1 and all it’s children.
Boston.Paint.Line1.** Gets Line1 and all of its children, recursively.
HighByte.Instance.* This is the default namespace, and represents the HighByte configuration. This queries all Instances in the configuration.
** Get the root of the namespace and all child nodes, recursively.

Where

Applies conditional logic to the From clause to filter the query. This is similar to a SQL WHERE clause. For example, you can filter for all instances modeled by Conveyor, or all Instances that have an attribute named state set to true.

Where Description
*[model="Conveyor"] Select all instances modeled after Conveyor.
*[value.state=true] Select all instances with an attribute named state set to true.
*[value.state=true and model="Conveyor"] Logically combine both queries using and/or.

Select

Controls what data is returned by the query. This is similar to a SQL SELECT clause, except it cannot be used to return specific attributes of an Instance, only the full value. The available options are described below.

Select Description
value Returns values in the namespace, similar to the test read results.
path Returns the paths in the namespace. For example, [Boston.Paint, Boston.Paint.Line1, ..].
name Returns the name of the source. For a modeled source this would be the Instance name.
model Returns the name of the model for each node, and an empty string if the data isn’t modeled.

Path Delimiter

Controls the Delimiter used when building the path from the namespace. By default this is a “.” (ex. Boston.Paint).

As

Controls the shape of the resulting read. For some workflows, hierarchy is required, and for others an array of values with a path attribute is preferred.

Note the difference between “Inline” and non-inline is the inclusion of a “value” node for attributes. See below examples.

As Option Description
Hierarchy The reads return the full hierarchy of the namespace.
List Returns an array of all the nodes.

Smart Query Metadata

The event generated by the stage includes metadata for path, name, and model under event.metadata.query. This is useful in cases where the metadata isn’t needed in the payload, but it is needed in the event to control the topic path the write occurs on, buffering, or other data flow needs.

Smart Query Examples

For the below examples, assume the Namespace looks as follows. Conveyor is based on the Conveyor model, and Tank on the Tank model.

js
{
    "Boston": {
        "Paint": {
            "Line1": {
                "Conveyor": {
                    "speed": 100, 
                    "state": 50
                },
                "Tank": {
                    "level": 50
                }
            }
        }
    }
}

Example As results

For these examples, Select is set to value only.

As: Hierarchy

js
{
	"Boston": {
		"Paint": {
			"Line1": {
				"Conveyor": {
                    "speed": 100,
                    "state": 50
				},
				"Tank": {
                    "level": 50
				}
			}
		}
	}
}

As: List

js
[
	{
    "speed": 100,
    "state": 50
	},
	{
    "level": 50
	}
]

Select all instances modeled after conveyor

Select: Boston.Paint.Line1.* Where: *[model = "Conveyor"] Select: value, path, model As: List

js
[
	{
        "speed": 100,
		"state": 50,
		"_path": "Boston.Paint.Line1.Conveyor",
		"_model": "Conveyor"
	}
]

Select all instances that have an active attribute set to true

Select: Boston.Paint.Line1.* Where: *[value.speed = 100] Select: value As: List

js
[
	{
    "speed": 100,
    "state": 50
	}
]