Our search endpoint returns three fields, and its WHERE clause is copied out of an index definition on purpose
Munchable's main input is a camera pointed at a barcode. That is useless to somebody writing a shopping list on the sofa, so there is also a search screen: type a name, tap a result, get the same answer you would have got in the aisle. The interesting thing about building it was how little the new code was allowed to know. This post is the route, the query, and the screen, in that order, with the…
Munchable's search endpoint is designed to return only three fields - barcode, productName, and brands. The endpoint does not have access to the full product data, so when a user taps on a search result, it triggers a POST request to /api/product-lookup using the barcode to fetch the complete product information. This architectural decision ensures that the search route does not have to manage product data or be aware of cache, trust status, or quota information.
The search process involves writing SQL fragments that define how to search the database. The searchable text is created by concatenating the productName and brands fields, separated by spaces. A partial GIN trigram index is created on this expression to improve search performance. The index predicate is also written as an SQL fragment and must match the query where clause exactly for the partial index to be effective.
Matching is done per word, rather than as a single phrase, allowing for more flexible search queries. Each word in the query is compared against the productName and brands fields, rather than treating the search term as a single phrase. This approach ensures that queries like "alpro oat" match the product named "Oat drink" correctly, even though they are searched for separately.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.