The searchCriteria parameter of a GET request allows for search results filtering. The key to using it comes down to understanding its structure and the available condition types.
Observing the \Magento\Framework\Api\SearchCriteriaInterface interface, and the Magento\Framework\Api\SearchCriteria class as its concrete implementation, we can easily conclude the following search_criteria structure:
"search_criteria": {
"filter_groups": [],
"current_page": 1,
"page_size": 10,
"sort_orders": []
}
Whereas the mandatory filter_groups parameter and its structure are shown as follows:
"filter_groups": [
{
"filters": [
{
"field": "fieldOrAttrName",
"value": "fieldOrAttrValue",
"condition_type": "eq"
},
{
// Logical OR
}
]
},
{
// Logical AND
}
],
Conditions nested under the individual filters key, correspond to the Logical OR condition.
The list of condition_type values includes:
- eq: Equals
- finset: A value within a set of values
- from: The beginning of a range, must be used with a to condition type
- gt: Greater than
- gteq: Greater than or equal
- in: In, the value can contain a comma-separated list of values
- like: Like, the value can contain the SQL wildcard characters
- lt: Less than
- lteq: Less than or equal to
- moreq: More or equal to
- neq: Not equal to
- nin: Not in; the value can contain a comma-separated list of individual values
- notnull: Not null
- null: Null
Combining these condition types will allow us to filter search results pretty much any way we want.
The optional sort_orders parameter and its structure unfold as follows:
"sort_orders": [
{
"field": "fieldOrAttrName",
"direction": "ASC"
}
]
The list of direction values includes ASC for ascending and DESC for descending sort orders.
The searchCriteria is seemingly the most complex, yet most powerful aspect of a search API. Understanding how it works is essential for effective querying.