Predicates provide a way to define complex expressions for querying resources or specifying conditional triggers for API Extensions.
The queryable APIs support ad-hoc filtering of resources through flexible predicates.
They do so via the
where query parameter that accepts a predicate expression to determine whether a specific resource representation should be included in the result.
API Extensions support predicates via the
condition field in the ExtensionTrigger.
Please note that the Query Predicates syntax differs from the ones used for other predicate types, most notably the predicates used to define discount targets.
The structure of predicates and the names of the fields follow the structure and naming of the fields in the documented response representation of the respective query results.
API endpoints that support Query Predicates allow passing input variables as separate HTTP query parameters.
Query Predicates by example
on standard and Custom Fields
on Product Attributes
A query endpoint usually restricts predicates to only be allowed on a specified subset of a resource representation's fields. The documentation of the endpoint lists fields that can be used for constructing predicates.
If multiple predicates are specified via multiple
where query parameters,
the individual predicates are combined in a logical conjunction, just as if
they had been specified in a single
where query parameter and combined with
Note: The encoding of the predicates is
UTF-8 and the predicate must be URL-encoded in the HTTP request.
Example predicate for querying Products:
The following fields on Customer can be used in Query Predicates:
Query predicates support the use of input variables to simplify working with query strings that contain dynamic values. Using input variables also eases log analysis because identical query use cases have identical
where query parameter values.
Inside the Query Predicate string, references to input variables must be prefixed with a colon
All input variables referenced in the Query Predicate must be added to the URI as separate HTTP query parameters whose names must be prefixed with
var.. The same input parameter can be passed multiple times to be used as an array of values.
The actual names of the input variables must consist of alphanumeric characters only.
Input variable examples
HTTP query using one input variable:
HTTP query using an array input variable:
Referencing input variables in Query Predicates:
Query predicates are translated to database queries whose efficiency depends on how well the database can use indexes.
Indexes are managed automatically by commercetools Composable Commerce. Some indexes are present on all projects, others are added dynamically. For example, if you add a custom field to your carts and start querying it, the system will add an index to the project to improve performance if it meets criteria like query frequency.
The automatic index creation needs to collect a significant amount of data to not optimize for outlier scenarios. That's why it can take up to two weeks before a new index is added.
Efficient queries can be fast on extremely large datasets and inefficient queries can be fast on small datasets, too. But inefficient query patterns on large datasets cause long-running and resource-intensive queries. Such queries can affect the overall performance of a Project.
Not all Query Predicates can be easily supported with an index, so if possible avoid the following patterns on large datasets:
- Indexes on Product Attributes on the Products and Product Projections endpoints are not provided. Use the Product Projection Search endpoint instead or try to narrow down your result set with an additional restriction, for example on the
- Fields nested inside arrays - the index becomes inefficient if it contains too many entries. For example,
variants(attributes(name = "attribute-name" and value = "attribute-value")).
- Querying for a condition that is true for the majority of resources, for example
custom(state = "Done").
- Negations, such as
state != "Open"or
state is not defined.
emptyoperator on arrays, such as
lineItems is emptyor
lineItems is not empty.
The following patterns are supporting efficient query execution:
- Non-nested fields that heavily reduce the subset of resources to filter, for example
custom(state = "WaitingForExternalApproval")(assuming there are few resources waiting for external approval)
- If possible, prefer equality over other operators. For example,
(state = "Done" or state = "Canceled)"can be faster than
(state != "Open")in a query that contains further expressions.
- Queries on Orders, Carts, Customers, etc. may be fast in the beginning, but slow down over time as your Project grows. Include a time range, for example
lastModifiedAt > $1-week-ago and ...(replace
$1-week-agowith an actual date). Try defaulting the time range to the smallest value that is acceptable for the use case. Alternatively, try filtering by a field value that naturally only occurs in recently created resources.
Sorting and query performance
Sorting can also be supported by indexes. For best performance, the same index can be used for filtering and sorting. If possible, re-use a field from the Query Predicate for sorting.
For example, if your filter query is
lastModifiedAt > $1-week-ago, sorting on
lastModifiedAt is advised since it is more performant than sorting on a different field, like
Deactivate calculating the total
Deactivating the calculation of the
total field in the PagedQueryResult will improve the performance of the query. Whenever the
total is not needed, deactivate its calculation by using the query parameter
Using predicates in conditional API Extensions
Besides querying resources, the predicates syntax also allows you to define complex expressions for the conditional execution of API Extensions.
A few minor differences aside, the behavior of the language and the operators used are the same for both querying APIs and defining API Extension conditions. The features not supported in conditional API Extensions are:
- Input variables
Using predicates to react to change in data
In addition to the existing set of predicates, conditional API Extensions support the ability to check for changes to a resource's properties. For example, if your Extension is configured to be triggered for update actions to Carts, you can check whether the update action contains changes to the Cart's shipping information. By using the
has changed operator, you can ensure that the Extension is called only when certain properties are updated in the update action triggering the Extension. Given a valid predicate, the
has changed operator will evaluate to true for all create actions.
The negation of
has changed is also supported. Using
has not changed will ensure that the Extension only triggers when certain properties do not change during the update action. Given a valid predicate, the
has not changed operator will evaluate to false for all create actions.
Below are a few examples on how the
has changed and
has not changed operators behave during create and update actions:
This feature requires a comparison between the current and previous versions of a resource during an update or create action. Therefore, the
has changed and
has not changed operators are only supported when defining API Extension conditions.
Fur further examples on how Query Predicates can be used in the context of API Extensions, you can find a detailed tutorial here.