Codewerk.
Get a quote
Home/Blog/The search criteria concept: Shopware's most underused feature

The search criteria concept: Shopware's most underused feature

Most integrations fetch far too much data because nobody read the criteria docs. Filters, aggregations and associations, explained by example.

Photo: free stock photography (Unsplash licence) — see imprint

One concept, three places

The same criteria object drives the DAL in PHP, the Admin API and the Store API. Learning it once pays off everywhere — and it is the difference between an integration that syncs in two minutes and one that times out.

Associations are opt-in for a reason

By default you get the entity, not its world. Ask for exactly the associations you render — and no more. 'Give me products with manufacturer, cover and prices' is fast; 'give me products with everything' will happily fetch half your database.

Aggregate instead of counting in PHP

If you are pulling 40,000 orders to count how many are unpaid, stop. An aggregation does it in the database and returns a number. This single habit removes most 'the export script kills the server' tickets.

Page it, always

No matter how small the catalogue is today, iterate with limit and offset — or better, with a cursor over a sorted key. Every integration that did not, eventually met a client with 300,000 products.

Key takeaways
  • Request only the associations you actually render.
  • Aggregate in the database, not in PHP.
  • Always paginate, even on a small catalogue.

Frequently asked questions

Nine times out of ten you are asking for too much. The criteria object lets you filter, sort, aggregate and pick associations — most integrations ignore it and pull whole entities with everything attached. That is the difference between a sync that finishes in two minutes and one that runs into a timeout. Learn it once: it is the same concept in the DAL, the Admin API and the Store API.

Because associations are opt-in. By default you get the entity, not its world — you have to ask for exactly the associations you render. That is deliberate, not an oversight: 'give me products with everything' will happily fetch half your database. List the ones your view actually needs, and nothing beyond them.

With an aggregation. If you are pulling forty thousand orders into PHP to count the unpaid ones, the database could have handed you that number directly. Aggregations run where the data lives and return a result instead of a result set. This one habit removes most of the tickets that begin with 'the export script kills the server'.

Yes, and write it now while it is cheap. Iterate with limit and offset, or better with a cursor over a sorted key. Every integration that skipped this eventually met a client with three hundred thousand products, usually on the day it mattered most. Today it is a few lines. Later it is a rewrite of the whole loop.

We do this for a living — Shopware, Node.js, React, ERP integration and automation for B2B.

Talk to an engineer

// Keep reading

Related articles