Search and Filter Together
What you'll learn: How to let users perform free-text search and apply structured filters simultaneously in one API request.
Why Combine Search and Filters?
Think of shopping on an e-commerce site: you might search for "running shoes" (text search) while filtering by size, color, and price range (structured filters). Users expect both to work together seamlessly, not as separate operations that cancel each other out.
How It Works in the Query String
Your API should accept both search parameters and filter parameters in the same URL:
GET /api/products?search=laptop&category=electronics&minPrice=500&maxPrice=1500
Here, search=laptop performs text matching across multiple fields (name, description), while category, minPrice, and maxPrice apply specific constraints.
Frontend Implementation
When building your fetch request, merge search and filter states:
const searchTerm = "laptop";
const filters = { category: "electronics", minPrice: 500 };
const params = new URLSearchParams({
search: searchTerm,
...filters
});
fetch(`/api/products?${params}`)
.then(res => res.json())
.then(data => console.log(data.results));
API Behavior
On the backend, both conditions apply together (logical AND):
- Match the search term anywhere in searchable fields
- AND match all filter criteria
This narrows results progressively—each added constraint reduces the dataset further.
UX Considerations
Show applied filters and the search term clearly in your UI. Users should be able to remove individual filters or clear the search independently without losing all their selections.
Key Takeaway: Combining search and filters in one API request gives users powerful, flexible ways to find exactly what they need—your query string should accept both and apply them together as intersecting conditions.