evitaDB - Fast e-commerce database
logo
page-background

Constant ordering

There are situations when the order of entities is specified outside evitaDB. The constant order constraints allow to control the order of the selected entities by a caller logic.

The constant ordering constraints are particularly useful when you have a sorted set of unique attributes or directly the entity primary keys provided by an external system that needs to be maintained in the evitaDB output (for example, it represents a relevance of those entities from the full-text engine).

Exact entity primary key order used in filter

The constraint allows output entities to be sorted by primary key values in the exact order used to filter them. The constraint requires the presence of exactly one entityPrimaryKeyInSet constraint in the filter part of the query. It uses the specified array of entity primary keys to sort the result returned by the query.

The sample query returns exactly 4 products, which maintain the order of the filtered primary keys in the query that was issued.

Exact entity primary key order

argument:int+

a mandatory set of entity primary keys that control the order of the query result

The constraint allows output entities to be sorted by entity primary keys in the exact order specified in the 2nd through Nth arguments of this constraint.

The sample query returns all products whose code starts with the string lenovo, but uses the order of the first three entities in the output as specified by the order constraint entityPrimaryKeyExact. Because the query returns more results than the order constraint has information for, the rest of the result set is sorted traditionally by the entity primary key in ascending order. If there is another order constraint in the chain, it would be used to sort the rest of the query result.

Exact entity attribute value order used in filter

argument:string!
a mandatory name of the attribute name that control the order of the query result
This constraint allows output entities to be sorted by values of the specified attribute in the exact order in which they were filtered. The constraint requires the presence of exactly one attribute-in-set in the filter part of the query, referring to the attribute with the same name as used in the first argument of this constraint. It uses the specified array of attribute values to sort the result returned by the query.
Chain attributes (Predecessor and ReferencedEntityPredecessor) cannot supply these filter values: they cannot be marked as filterable, so the required attributeInSet filter is refused. Use attributeNatural to order by the chain instead.
The sample query returns exactly 3 products, preserving the order of the entity's code attribute used in the filter constraint of the query that was issued.

Exact entity attribute value order

argument:string!
a mandatory name of the attribute name that control the order of the query result
argument:comparable+
a mandatory set of attribute values whose data type matches attribute data type, which define the order of the query result

The constraint allows output entities to be sorted by attribute values in the exact order specified in the 2nd through Nth arguments of this constraint.

Chain attributes of type Predecessor or ReferencedEntityPredecessor are refused when the query is translated, both at entity level and inside referenceProperty orderings. A chain orders by predecessor links, so its values cannot be listed in an exact value order. Use attributeNatural to follow the chain instead. Inside referenceProperty, that needs traverseByEntityProperty or no traversal constraint at all, because pickFirstByEntityProperty cannot order by a chain.
Inside a referenceProperty ordering with traverseByEntityProperty, attributeSetExact orders each block of the traversal separately (see how the entities are placed). In each block, the listed values come first, in the listed order, with ties by ascending primary key. An entity whose value is not listed, or that has no value, passes to the next ordering constraint inside referenceProperty; if none of them places it, it waits for a later block. Entities that no block places continue to the next ordering constraint of the query.
The value of an entity is taken from its first reference to the block's entity that has a value - in the order of the representative attribute values when there are several. A later reference with a listed value does not replace an earlier one with an unlisted value.
Under pickFirstByEntityProperty, attributeSetExact works only when the query narrows the ordered reference down to a single referenced entity, for example by filtering for it with referenceHaving. The same applies to attributeSetInFilter inside any reference ordering.
The sample query returns all products whose code starts with the string lenovo, but uses the order of the first three entities in the output as specified by the order constraint attributeSetExact. Because the query returns more results than the order constraint has information for, the rest of the result set is sorted traditionally by the entity primary key in ascending order. If there is another order constraint in the chain, it would be used to sort the rest of the query result.

Author: Ing. Jan Novotný

Date updated: 17.1.2023

Documentation Source