GET /v1/restaurants: search by name
You add a q parameter to GET /v1/restaurants that finds restaurants by whole words or word prefixes in their names. Every searchable word must match, while geographic filtering and pagination remain intact.
Ticket
You add optional name search to GET /v1/restaurants using a MySQL FULLTEXT index. The q parameter matches whole words and the starts of words in restaurant names, without case sensitivity. For example:
GET /v1/restaurants?lat=19.0760&lng=72.8777&radius_km=5&limit=50&q=pizFor the W2O2 fixture, the matching items[].name values are Pizza Palace, Pizzeria Roma, Dominos Pizza and Late Night Pizza Corner. A query such as pizza palace must require both words, and punctuation in q must not become search syntax.
Customers need to find restaurants by name without losing the geographic and active-status filters from W2B4. Preserve W2O1's signed cursor behaviour and ID-based pagination. The next optional extension builds on this listing endpoint.
Endpoint
Active restaurants near a point, one page at a time
latnumberrequired-90 to 90
lngnumberrequired-180 to 180
radius_kmnumberabove 0, at most 50, default 5
limitintegerdefault 20. Integers below 1 count as 1 and above 50 as 50.
cursorstringnext_cursor from the previous page of the same query.
atstring (date-time)RFC 3339 time that is_open is evaluated at. Defaults to now.
qstringWords to search restaurant names for. A restaurant matches when every word matches a whole word in its name or the start of one, ignoring case. Operators such as + - * " are plain text. The geo filter, active filter and paging still apply.
itemsRestaurantSummary[]requiredidintegerrequirednamestringrequiredlatnumberrequiredlngnumberrequiredis_openbooleanrequiredWhether one of the restaurant's open_slots contains the request's at time, in Asia/Kolkata local minutes. A slot covers open_min up to but not including close_min; when close_min is not after open_min the slot runs past midnight. Before the hours are first loaded this is false.
next_cursorstringrequiredEmpty on the last page.
invalid_parameterinvalid_cursor
Acceptance criteria
- A new migration, version
0003, creates a FULLTEXT index onrestaurants.name. The index is discoverable throughinformation_schema.statisticswithindex_type = 'FULLTEXT'. - Searching for
pizzareturnsPizza Palace,Dominos PizzaandLate Night Pizza Corner. Searching forpizalso returnsPizzeria Roma, while searching foralacereturns no results. - Search is case-insensitive.
PIZZAreturns the same results aspizza, andbUrGeRreturnsBurger Barn. - Every searchable word must match.
pizza palacereturns onlyPizza Palace,night cornreturns onlyLate Night Pizza Corner, andpizza burgerreturns no results. - Boolean operator characters in
q, including",-,+,*and parentheses, are treated as plain text. The tested queries must not produce malformed-search errors or change their expected prefix-search results. - Searching for
pizzapreserves geographic and active-status filtering.Pizza ClosedandPizza Farawaydo not appear in the results. - With
q=pizandlimit=3, followingnext_cursorvisits all four matching restaurants exactly once, across two pages containing three and one results. - A
qwith no searchable Unicode letter or digit, or with more than 100 characters, returns400 Bad Requestwith error codeinvalid_parameterand fieldq.
Constraints
- Add the FULLTEXT index on
restaurants.namein migration version0003. - Limit
qto 100 characters. - Build search terms from Unicode letters and digits. Require every resulting word to match, including as a word prefix.
- Treat user-supplied boolean operator characters as plain text.
- Preserve the existing geographic filters, active-status filter and keyset pagination.
Design direction
A useful split keeps the database index and query separate from the code that interprets user input. Add the index through backend/db/migrations/, then define the parameterized FULLTEXT query in backend/db/queries/. Use sqlc to generate the database query code.
Keep search-expression construction in the restaurant layer. Validate q in the HTTP handler, then pass the validated query into the listing service. Keep search optional so requests without q continue using the existing listing path.
Consider these trade-offs:
- Search safety: MySQL boolean mode gives characters such as
+,-and*special meaning. A parameterized SQL query protects the SQL statement, but does not stop those characters from changing search semantics. Extract Unicode letters and digits before constructing search terms. - Matching rules: Use Boolean mode search to require each word and support prefixes. A FULLTEXT index makes the name search available to MySQL without relying on a leading-wildcard
LIKEquery. - Pagination stability: Keep results ordered by restaurant ID. Ordering by relevance would require additional cursor state to handle equal scores. ID ordering preserves the existing Keyset pagination behaviour, including signed cursors.
Any internal layout that meets the contract passes.
How to check your work
Run these commands from the repository root:
cd backend && make up
cd backend && make test W=2O2Read the first failing assertion to identify whether the problem is name matching, input validation, filtering, pagination or the FULLTEXT index.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Add a FULLTEXT index on
restaurants.namein migration version0003. - Match whole words and word prefixes without case sensitivity.
- Require every searchable word and treat boolean operator characters as plain text.
- Preserve the geographic and active-status filters.
- Preserve ID-based pagination and W2O1 signed cursor behaviour.
- Reject queries with no searchable characters or more than 100 characters using
400 invalid_parameterwith fieldq.
Commands
cd backend && make upcd backend && make test W=2O2Tests
Searching matches complete words and their prefixes, but not arbitrary substrings inside words.
GET/v1/restaurants?lat=19.0760&lng=72.8777&radius_km=5&limit=50&q=pizParamslat 19.0760 lng 72.8777 radius_km 5 limit 50 q piz Response 200 OK{ "items": [ { "id": …, "name": "Pizza Palace", … }, … ], "next_cursor": "" }Input Search the fixture restaurants using
pizza,pizandalace.Expected
pizzareturns Pizza Palace, Dominos Pizza and Late Night Pizza Corner.pizadditionally returns Pizzeria Roma.alacereturns no results.TestW2O2_AWordMatchesWholeWordsAndWordStartsinbackend/e2e/w2_o2_test.go