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:

http
GET /v1/restaurants?lat=19.0760&lng=72.8777&radius_km=5&limit=50&q=piz

For 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

GET/v1/restaurants

Active restaurants near a point, one page at a time

Query parameters
  • latnumberrequired

    -90 to 90

  • lngnumberrequired

    -180 to 180

  • radius_kmnumber

    above 0, at most 50, default 5

  • limitinteger

    default 20. Integers below 1 count as 1 and above 50 as 50.

  • cursorstring

    next_cursor from the previous page of the same query.

  • atstring (date-time)

    RFC 3339 time that is_open is evaluated at. Defaults to now.

  • qstring

    Words 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.

Response 200 OK
  • itemsRestaurantSummary[]required
    • idintegerrequired
    • namestringrequired
    • latnumberrequired
    • lngnumberrequired
    • is_openbooleanrequired

      Whether 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_cursorstringrequired

    Empty on the last page.

Response 400 Bad Request

invalid_parameterinvalid_cursor

Acceptance criteria

  • A new migration, version 0003, creates a FULLTEXT index on restaurants.name. The index is discoverable through information_schema.statistics with index_type = 'FULLTEXT'.
  • Searching for pizza returns Pizza Palace, Dominos Pizza and Late Night Pizza Corner. Searching for piz also returns Pizzeria Roma, while searching for alace returns no results.
  • Search is case-insensitive. PIZZA returns the same results as pizza, and bUrGeR returns Burger Barn.
  • Every searchable word must match. pizza palace returns only Pizza Palace, night corn returns only Late Night Pizza Corner, and pizza burger returns 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 pizza preserves geographic and active-status filtering. Pizza Closed and Pizza Faraway do not appear in the results.
  • With q=piz and limit=3, following next_cursor visits all four matching restaurants exactly once, across two pages containing three and one results.
  • A q with no searchable Unicode letter or digit, or with more than 100 characters, returns 400 Bad Request with error code invalid_parameter and field q.

Constraints

  • Add the FULLTEXT index on restaurants.name in migration version 0003.
  • Limit q to 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 LIKE query.
  • 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:

bash
cd backend && make up
cd backend && make test W=2O2

Read the first failing assertion to identify whether the problem is name matching, input validation, filtering, pagination or the FULLTEXT index.

Finished Exercise 2.24?

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Add a FULLTEXT index on restaurants.name in migration version 0003.
  2. Match whole words and word prefixes without case sensitivity.
  3. Require every searchable word and treat boolean operator characters as plain text.
  4. Preserve the geographic and active-status filters.
  5. Preserve ID-based pagination and W2O1 signed cursor behaviour.
  6. Reject queries with no searchable characters or more than 100 characters using 400 invalid_parameter with field q.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=2O2

Tests

  1. 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=piz
    Params
    lat19.0760
    lng72.8777
    radius_km5
    limit50
    qpiz
    Response 200 OK
    {
      "items": [ { "id": …, "name": "Pizza Palace", … }, … ],
      "next_cursor": ""
    }

    Input Search the fixture restaurants using pizza, piz and alace.

    Expected pizza returns Pizza Palace, Dominos Pizza and Late Night Pizza Corner. piz additionally returns Pizzeria Roma. alace returns no results.

    TestW2O2_AWordMatchesWholeWordsAndWordStarts in backend/e2e/w2_o2_test.go