Searching inside text
To find the records whose text holds what someone typed, ignoring case,
call text.contains_ignoring_case on the field with the text the query
is given. It is the one operation whose second argument may be a value
given when the query runs, so a search box's text goes straight in:
use notes/entities { User }use notes/types { NoteTitle }/// Something a user wrote down for themselves.entity Note { id: Uuid owner_id: User.id { on_delete: OnDelete.cascade } title: NoteTitle policy: { create: [owner_id] read: [owner_id] }}
use notes/types { NoteTitle }/// What a list of notes is narrowed to: the text someone typed, if any.type FindNotes = Input { q: NoteTitle { optional: true }}
use notes/entities { Note }use notes/types { FindNotes, NoteView }/// The caller's notes, those whose title holds the text when one is given.query my_notes(asked: FindNotes) -> List<NoteView> { from: Note where_present: text.contains_ignoring_case(Note.title, asked.q) order: [Note.title asc] limit: 50}
The search ignores case, reads % as a percent sign, and finds only the
caller's own notes, since the read's policy still applies:
use notes/entities { Note, User }use notes/types { FindNotes, NoteView }use notes/actions { write_note }/// What the list shows of a note.function card_of_note(note: Note) -> NoteView { return NoteView { id: note.id title: note.title }}test "a search ignores case, reads its text literally and finds only the caller's notes" { var ada: User = make.User { email: "ada@example.com" } var bob: User = make.User { email: "bob@example.com" } var milk: Note = write_note(Principal.user(ada), "Buy MILK") var oat: Note = write_note(Principal.user(ada), "oat milk") var half_price: Note = write_note(Principal.user(ada), "50% off") var stock: Note = write_note(Principal.user(ada), "5000 items") var eclairs: Note = write_note(Principal.user(ada), "ÉCLAIRS for Friday") var bobs: Note = write_note(Principal.user(bob), "Bob's milk") var with_milk: Response = request { method: HttpMethod.get path: "/notes" as: Principal.user(ada) input: FindNotes { q: "milk" } } assert with_milk.output == [card_of_note(milk), card_of_note(oat)] var with_percent: Response = request { method: HttpMethod.get path: "/notes" as: Principal.user(ada) input: FindNotes { q: "50%" } } assert with_percent.output == [card_of_note(half_price)] var with_eclair: Response = request { method: HttpMethod.get path: "/notes" as: Principal.user(ada) input: FindNotes { q: "éclair" } } assert with_eclair.output == [card_of_note(eclairs)] var all_of_adas: Response = request { method: HttpMethod.get path: "/notes" as: Principal.user(ada) input: FindNotes {} } assert all_of_adas.output == [ card_of_note(half_price), card_of_note(stock), card_of_note(milk), card_of_note(oat), card_of_note(eclairs) ] // by code point var for_bob: Response = request { method: HttpMethod.get path: "/notes" as: Principal.user(bob) input: FindNotes { q: "MILK" } } assert for_bob.output == [card_of_note(bobs)]}
- What it keeps. The records
text.contains_ignoring_casekeeps in code: the field holds the text somewhere, each code point compared by Unicode's simple case folding (Case and sorting in a locale), somilkfindsBuy MILKandéclairfindsÉCLAIRS. The text is literal:%,_and\are themselves, never a pattern, so50%doesn't find5000. Empty text is in every text. - The value. A parameter, a field of one, or text written out. The index holds the field's text, never the search's, so a value given when the query runs changes nothing the index holds.
- Where it stands. On a
where:line of its own or joined byand, in an include'swhere:and in an aggregate query, as ageo.test does. On awhere_present:line it applies only when the text is there, asasked.qis above. Not undernotoror, and not compared with== true(query.condition_unsupported): the index finds every record the search may keep, never those it doesn't. - How the database finds them. The statement searches the field's
text with its ASCII letters lowered,
lower("title" COLLATE "C") LIKE $2 ESCAPE E'\\', through a trigram index, and Jadpo's own code decides each record it finds, as it does for ageo.test.$2is the text made a pattern when the query runs:%,_and\escaped, ASCII letters lowered, and every other code point matched by_, as arekands, whichK(the Kelvin sign) andſfold to. So the pattern finds every record the code keeps, whatever the database's collation, and perhaps a few more, which the code leaves out. - The index. The compiler derives one GIN index of trigrams,
PostgreSQL's
pg_trgm, on each field searched this way, however many queries search it:<table>_<column>_trgm_idx, partial withvisible:as every derived index is. The migration first creates thepg_trgmextension, which ships with PostgreSQL and which the database's owner may create. Text of fewer than three characters holds no trigram, so a search that short reads every record the other conditions leave. - Other operations. Every other operation's arguments are still
written out (Operations in conditions), so
text.contains(Note.title, asked.q)isquery.condition_unsupported, itsnext:namingtext.contains_ignoring_case. Words, stems and ranking arematches(Search). - Redis. An entity kept in Redis isn't searched this way
(
query.redis_unsupported), since Redis keeps no index of trigrams.
The statement (jadpo sql) and the index derived for it:
-- my_notesSELECT "id", "title"FROM "note"WHERE ($1 IS NULL OR lower("title" COLLATE "C") LIKE $2 ESCAPE E'\\') AND "note"."owner_id" = $3ORDER BY "title" COLLATE "C" ASC, "id" ASCLIMIT $4CREATE EXTENSION IF NOT EXISTS "pg_trgm";CREATE INDEX "note_title_trgm_idx" ON "note" USING gin ((lower("title" COLLATE "C")) gin_trgm_ops);
Markdown for agents: /docs/searching-inside-text.md.Built from Jadpo's main branch at commit 9080dc3.