DataLab secondary queries


A secondary selection runs once for each Entity a primary selection finds, and its criteria can compare against that primary Entity. Eg: for each pump, the valves within 50 metres of it.

In Operator a saved query gets a secondary selection by choosing "Reference in a secondary selection" as its action. The pairs it finds can then be related, merged, counted, or used to copy values across.

This article will cover how a secondary selection references the primary one, how to search it, and the actions that use both. Selections and criteria are described in DataLab queries, and running actions in DataLab actions.

Referencing the primary Entity

A secondary selection has the same shape as a primary one. These criteria can read the primary Entity:

// An attribute compared to an attribute of the primary Entity.
{
    "key": "attribute",
    "AttributeValue": {
        "AttributePath": "\"assetCode\"",
        "Operator": "=",
        "ValueOption": "PRIMARY",
        "OperandA": { "AttributePath": "\"assetCode\"" }
    }
}

// Within 50 metres of the primary Entity's location.
{
    "key": "attribute",
    "AttributeValue": {
        "AttributePath": "\"Bruce\"/\"Location\"",
        "Operator": "NEAR BY",
        "Distance": 50,
        "ValueOption": "PRIMARY",
        "OperandA": { "AttributePath": "\"Bruce\"/\"Location\"" }
    }
}

// The Principal of a relation to the primary Entity.
{
    "key": "relation",
    "Relation": { "Operator": "primary", "Type": "my-relation-type-id" }
}

// Related to the primary Entity.
{
    "key": "related",
    "Related": { "RelationTypeID": "my-relation-type-id", "ValueOption": "PRIMARY" }
}

Set "LimitToPrimary" to true on the secondary selection to keep only secondary matches that the primary selection also finds. Eg: pairs of Entities from the same set.

Searching a secondary selection

Send both selections to "v3/datalab/runSearch". The secondary one is what runs. Without "PrimaryEntityIDs", "Skip" and "Load" page through the primary Entities, and the unique secondary matches are capped at "Load". With "PrimaryEntityIDs", only those primary Entities are used, and the result is capped at 10,000 with a warning.

The response's "Items" are the secondary Entities, and "Mapping" lists the secondary IDs found for each primary ID. Counting a secondary selection needs "PrimaryEntityIDs", and "IndexOf" is not available.

Run a secondary search

Request body
interface IPostBody {
    "PrimarySelection": ISelection;
    "SecondarySelection": ISelection;
    // Primary Entity IDs to find secondary matches for.
    "PrimaryEntityIDs"?: string[];
    // A single primary Entity ID, used when PrimaryEntityIDs is not given.
    "PrimaryEntityID"?: string;
    // Default is 0.
    "Skip"?: number;
    // Default is 50.
    "Load"?: number;
}
Response
interface IResponse {
    // The secondary Entities.
    "Items": any[];
    // Each primary Entity ID to the IDs of its secondary matches.
    "Mapping"?: { [primaryEntityId: string]: string[] };
    // Number of unique secondary matches, when Skip and Load were both -1.
    "Count"?: number;
    "Error": string[];
    "Warning": string[];
}

Action: Set attribute

With a secondary selection, "set-attribute" can copy values between the two sides. "ValueOption" PRIMARY reads the value from an attribute, so "Value" carries an "AttributePath" instead of a literal.

interface ISetAttributeAction {
    "ActionType": "set-attribute";
    // Attribute path to write.
    "Path": string;
    "AttributeType": "string" | "integer" | "double" | "datetime" | "structure" | "geometry";
    // PRIMARY reads Value.AttributePath from the other side. CONST writes Value as it is.
    // Default is CONST.
    "ValueOption"?: "CONST" | "USER" | "PRIMARY";
    "Value": string | { "AttributePath": string };
    /**
     * Which side is written.
     * Default is SECONDARY when ValueOption is PRIMARY, and PRIMARY otherwise.
     * A literal written to PRIMARY only reaches primary Entities that have a secondary match.
     */
    "Target"?: "PRIMARY" | "SECONDARY";
    // Which side a PRIMARY value is read from.
    // Default is PRIMARY. SECONDARY is only allowed with Target PRIMARY.
    "ValueFrom"?: "PRIMARY" | "SECONDARY";
    /**
     * How several secondary values become the one value a primary Entity holds.
     * Default is FIRST.
     * MIN, MAX and SUM apply to numbers and keep the first value otherwise.
     */
    "ValueReduce"?: "FIRST" | "LAST" | "MIN" | "MAX" | "SUM";
    // Several attributes in one pass. Each entry has its own Path, AttributeType, ValueOption, Value and ValueReduce.
    // Target and ValueFrom stay on the action.
    "Attributes"?: any[];
}

// Eg: write each pump's largest valve pressure onto the pump.
{
    "ActionType": "set-attribute",
    "Path": "\"maxValvePressure\"",
    "AttributeType": "double",
    "ValueOption": "PRIMARY",
    "Value": { "AttributePath": "\"pressure\"" },
    "Target": "PRIMARY",
    "ValueFrom": "SECONDARY",
    "ValueReduce": "MAX"
}

Action: Relate

Creates a relation of one Relation Type between each primary Entity and each of its secondary matches. You can read about relations in Entity relations.

interface IRelateAction {
    "ActionType": "relationship-map";
    // Required. The Relation Type ID.
    "RelationshipMappingID": string;
    // Which side is the Principal.
    // Default is "primary-to-secondary", where the primary Entity is the Principal.
    "Direction"?: "primary-to-secondary" | "secondary-to-primary";
    // Copies the primary Entity's location to its secondary matches.
    // Default is false.
    "CopyLocation"?: boolean;
    // With CopyLocation, calculates assembly locations first. This is slow.
    // Default is false.
    "CalcAssemblyLocations"?: boolean;
}

Action: Delete relations

Deletes the relations of one Relation Type between each primary Entity and its secondary matches. Note the default direction differs from relating.

interface IRelationsRemoveAction {
    "ActionType": "relationships-remove";
    // Required.
    "RelationType.ID": string;
    // Which side is the Principal.
    // Default is "secondary-to-primary", where the secondary Entity is the Principal.
    "Direction"?: "primary-to-secondary" | "secondary-to-primary";
    // Only deletes relations in this dimension.
    // Default is every dimension.
    "Dimension.ID"?: number;
}

Action: Merge

Updates each primary Entity from its secondary matches by a Data Mapping, with each secondary Entity as the source and the primary Entity as the target. The secondary Entities are not deleted. You can read about the mapping in Data Mapping.

interface IMergeAction {
    "ActionType": "merge";
    // Required.
    "DataMapping": IDataMapping;
}

Action: Save secondary count

Writes the number of secondary matches onto each primary Entity.

interface ISaveSecondaryCountAction {
    "ActionType": "save-secondary-items-count";
    // Required. Attribute path on the primary Entity to write the count to.
    "Path": string;
    // Leaves a secondary match out of the count when it is the primary Entity itself.
    // Default is false. "on" and "off" also work.
    "SkipPrimarySecondarySelfMatch"?: boolean | "on" | "off";
    // DIRECT writes the count straight to the stored attribute.
    // COMPREHENSIVE loads and saves each Entity, which is slower but runs the usual save logic.
    // Default is DIRECT.
    "SaveType"?: "DIRECT" | "COMPREHENSIVE";
}