Shopware 6

Search Criteria

Overview

https://developer.shopware.com/docs/guides/integrations-api/general-concepts/search-criteria Generally, we refer to the endpoints that use the POST method and receive the criteria as a JSON object as search criteria. It takes the same arguments as a DAL criteria. Some endpoints expect more parameters than specified here; these differ from one endpoint to another, so they are not specified here.

A typical search criteria looks like this:

In the following, we will go through different parameters that criteria can be assembled from.

Parameter
Usage

associations

Allows loading additional data to the standard data of an entity (similar to a SQL Join).

includes

Restricts the output to the defined fields, reducing the response payload.

ids

Limits the search to a specific list of record IDs.

total-count-mode

Defines how the total number of search hits should be determined (exact, no total, or next page check).

page

Defines the starting page for the search result (1-indexed).

limit

Defines the maximum number of entries to be returned.

filter

Allows filtering the result and aggregations using various filter types.

post-filter

Allows filtering the result but not the aggregations.

query

Enables determining a weighted ranking (_score) for the search result.

term

Enables a simple text search on all records based on their data model weighting.

sort

Defines the sorting of the search result, including field, order, natural sorting, and type.

aggregations

Specify aggregations (e.g., average price, counts) to be computed on-the-fly.

grouping

Allows grouping records by specified fields (e.g., one product per manufacturer).

includes (apiAlias)

The includes parameter allows you to restrict the returned fields.

  • Transfer only what you need reduces response payload.

  • Easier to consume for client applications.

  • When debugging, the response is smaller, and you can concentrate on the essential fields.

Example:

Response:

All response types come with an apiAlias field which you can use to identify the type in your includes field. If you only want a category id, add: "category": ["id"]. For entities, this is the entity name: product, product_manufacturer, order_line_item, ... For other non-entity types like a listing result or a line item, check the full response. This pattern applies not only to simple fields but also to associations.

ids

If you want to perform a simple lookup using just the ids of records, you can pass a list of those using the ids field:

total-count-mode

The total-count-mode parameter can be used to define whether the total number of hits should be determined for the search query. This parameter supports the following values:

  • 0 [default] - No total is determined.

    • Advantage: Most performant because MySQL does not need to run SQL_CALC_FOUND_ROWS.

    • Purpose: Use if pagination is not required.

  • 1 - An exact total is determined.

    • Purpose: Use if paginating with exact page numbers.

    • Disadvantage: Performance intensive; uses SQL_CALC_FOUND_ROWS.

  • 2 - It is determined whether there is a next page.

    • Advantage: Good performance, same as 0.

    • Purpose: Useful for infinite scrolling to know if there is a next page.

Example:

page & limit

The page and limit parameters control pagination. The page parameter is 1-indexed.

filter

The filter parameter allows you to filter the result and aggregations using many filters and parameters. The filter types are equivalent to the filters available for the DAL.

Filters Reference:

Name
Notes

equals

Exact match for the given value

equalsAny

At least one exact match for a value of the given list

contains

Before and after wildcard search for the given value

range

For range compatible fields like numbers or dates

not

Allows to negate a filter

multi

Allows to combine different filters

prefix

Before wildcard search for the given value

suffix

After wildcard search for the given value

Equals

The equals filter checks fields for an exact value. Example SQL: WHERE stock = 10.

EqualsAny

The equalsAny filter allows matching any value from a list. Example SQL: WHERE productNumber IN (...).

Contains

The contains filter performs a wildcard search. Example SQL: WHERE name LIKE '%Lightweight%'.

Range

The range filter supports numeric or date ranges. Parameters: gte, lte, gt, lt. Example SQL: WHERE stock >= 20 AND stock <= 30.

Not

The not filter is a container to negate filters. The operator (or or and) defines how queries inside are combined. Example SQL: WHERE !(stock = 1 OR availableStock = 1) AND active = 1.

Multi

The multi filter is a container to combine filters with a logical operator (or or and). Example SQL: WHERE (stock = 1 OR availableStock = 1) AND active = 1.

Prefix

The prefix filter checks that the field starts with a value. Example SQL: WHERE name LIKE 'Lightweight%'.

Suffix

The suffix filter checks that the field ends with a value. Example SQL: WHERE name LIKE '%Lightweight'.

When you are filtering for nested values — for example, filtering orders by their transaction state (order.transactions.stateMachineState) — make sure to fetch those in your associations field first.

Example nested associations and filter:

post-filter

Works the same as filter; however, post-filter does not apply to aggregations. This is useful when you want aggregations (facets) to be computed on a broader set but display results filtered without an extra request.

query

Use query to create a weighted search that returns a _score for each entity. Any filter type can be used for the query. A score must be defined for each query; the sum of matching queries results in the total _score.

Example:

Resulting _score is appended to each resulting record in extensions.search:

term

Using term, the server performs a text search on all records based on their data model and weighting defined with the SearchRanking flag.

Don't use term together with query.

Example:

Results are formatted the same as for the query parameter.

sort

The sort parameter controls sorting. Several sort entries can be provided.

  • field: field used for sorting.

  • order: sort direction (ASC or DESC).

  • naturalSorting: enables natural sorting algorithm.

  • type: allows divergent sorting behavior. Valid value shown below:

    • count: Sort by the count of associations via the given field. SQL: ORDER BY COUNT({field}) {order}

Example:

count sorting behavior

Example request payload that includes a count aggregation.

This count type was introduced with Shopware 6.4.12.0 and is not available in prior versions.

In response, the order of the product elements equals the order of the aggregated buckets:

aggregations

The aggregations parameter can determine metadata for a search query (statistics, facets). Aggregation types are equivalent to the DAL aggregations.

Reference: resources/references/core-reference/dal-reference/aggregations-reference.md

Example — average price:

grouping

The grouping parameter allows grouping results over fields. Use cases:

  • Fetch one product per manufacturer.

  • Fetch one order per day and customer.

Example:

Zuletzt aktualisiert

War das hilfreich?