# Search ## What this builds A search box for the records in an app. It uses the full-text search the database already has, so there is nothing else to run. What a person types is never treated as query syntax, results come best first, and nobody finds a record they may not open. Read `spekkr.inputs.json` first. Text is treated as `language`. `prefix_matching` changes R3 and `highlights` decides whether R5 exists. The spec searches one example table, `notes`, described as data in `design.md`. To search something else, change that description. It needs the app to know who is signed in. ## Out of scope - A separate search service. - Correcting spelling, and finding words by meaning. - Searching inside uploaded files. ## Requirements Each requirement has an id. Tests name the id they cover. ### R1. What can be found - One description says what is searched: the table, the fields with a weight each, the field to show as a title, and the rule for who may see a row. - A record can be found as soon as the transaction that saved it commits. The index is kept by the database, not by app code that could forget. Accept when: a record saved in a transaction is found right after the commit, and one in a rolled-back transaction never is. ### R2. Read what was typed - What a person types is turned into terms by the rules in `design.md`: words, phrases in double quotes, and words to leave out, marked with a minus sign. - Nothing typed can reach the database as query syntax. Punctuation separates words and means nothing else. - A query is cut to 200 characters and to its first 8 terms. - A query with no terms searches nothing and answers with no results. Accept when: every row of the table of cases in `design.md` gives the terms listed, and none of the cases raises an error from the database. ### R3. Match - A record matches when it holds every word and every phrase, and none of the words to leave out. - Words match by their stem in `language`: `invoices` finds `invoice`. - If `prefix_matching` is on, the last word, when it is not in quotes, also matches words that start with it: `ret` finds `return`. Accept when: each of the three rules has a test with a record that matches and one that does not. ### R4. Order - Results come best first. A record with the words in its title comes before one with them only in its body. - Records that rank the same come newest first, then by id, so the order never changes between two identical searches. Accept when: with one record of each kind the title match is first, and two searches a second apart return the same order. ### R5. Show where it matched Only if `highlights` is on. - Each result carries a piece of its text of at most 200 characters around the first match, with the matching words marked. - The text is escaped as HTML before the marks are added. A record that holds `