Query filtering determines which records qualify for a result. A filter can compare a field to a value, check whether a value falls within a range, or combine conditions with AND and OR. For example, an order service might return only shipped orders created on or after a specified date. Filtering is distinct from projection, which chooses the fields to return, and sorting, which determines their order. Define how filters handle null or missing values, case sensitivity, and date boundaries so callers know what will match.
When exposing an entity through an API, define which fields are filterable, which operators each field supports, and how multiple conditions are combined. A REST endpoint might accept GET /orders?status=shipped, while a GraphQL schema might expose a typed filter argument on its orders field. These are interface conventions, not capabilities that REST or GraphQL provides automatically. Validate field names, operators, and value types, and translate accepted conditions into native predicates with bound values rather than concatenating raw query fragments. A client-supplied filter is not an authorization rule: enforce tenant and row-level restrictions independently, then combine them with the requested conditions.
Push filters to the database, search engine, or analytics engine where practical. An index on tenant, status, and creation time can help a relational engine narrow a scan when the query specifies the tenant and status and supplies a date range. But returning fewer rows does not guarantee that fewer rows were read. An unindexed predicate or a function applied to an indexed field can still trigger a broad scan. Inspect query plans or scanned-data metrics, and add indexes for common filter patterns only when the read benefit justifies the extra storage and write work.
Apply filters before pagination so a page contains matching records rather than whatever survives after a page of unfiltered results is fetched. Use a stable sort with a unique tie-breaker, and reject cursor reuse when the filter or sort changes. Search and filtering can share a query but serve different purposes: a text search might rank products for running shoes, while a stock filter excludes unavailable products from those results. When caching a filtered response, key it by the effective filter, sort, page, returned fields, and any authorization scope that changes the response. Cache common combinations selectively; many one-off filter combinations produce few hits, and cached results can become stale when records change.