package repository import ( "fmt" "strings" ) // SearchFacets filters document search on extracted facts (V3 S4). // Empty = no filter. From/To are ISO dates (YYYY-MM-DD) applied to // date/due_date facts and overlapped with period facts (YYYY-MM). type SearchFacets struct { Issuer string From, To string MinAmount, MaxAmount *float64 DocType string } // searchFileColumns mirrors fileColumns qualified for `resources r`. const searchFileColumns = `r.resource_id, r.name, r.size_bytes, COALESCE(r.mime_type, ''), COALESCE(r.parent_id, ''), r.created_at, r.updated_at, COALESCE(r.ocr_text, ''), COALESCE(r.has_content, FALSE)` // SearchDocumentsVisible returns the user's accessible files (owned or // shared, viewer+) matching q across name (ILIKE, compat), full-text // (french TS, name+OCR) and extracted facts (ILIKE on value_text), // optionally narrowed by facets. Ranked: TS rank + fact bonus (high- // confidence fact match +1.0, low-confidence +0.5 — humble by design : // low-confidence demotes, never excludes), then name. func (r *Resources) SearchDocumentsVisible(userID, q string, facets SearchFacets, limit, offset int) ([]FileRow, int, error) { where, order, args := r.searchDocumentsClauses(userID, q, facets) base := `WITH RECURSIVE ` + grantedCTE + ` SELECT %s FROM resources r WHERE ` + where var total int if err := r.DB.QueryRow(fmt.Sprintf(base, "COUNT(*)"), args...).Scan(&total); err != nil { return nil, 0, err } rows, err := r.DB.Query( fmt.Sprintf(base+` ORDER BY %s, r.name ASC LIMIT $%d OFFSET $%d`, searchFileColumns, order, len(args)+1, len(args)+2), append(args, limit, offset)..., ) if err != nil { return nil, 0, err } defer rows.Close() files := make([]FileRow, 0) for rows.Next() { row, err := r.scanFile(rows.Scan) if err != nil { return nil, 0, err } files = append(files, row) } return files, total, rows.Err() } // searchDocumentsClauses builds the WHERE (match) and ORDER BY (rank) // fragments plus bound args. $1 is always userID (grantedCTE). func (r *Resources) searchDocumentsClauses(userID, q string, facets SearchFacets) (where, order string, args []any) { args = []any{userID} next := 2 ph := func(v any) string { args = append(args, v) p := fmt.Sprintf("$%d", next) next++ return p } // plainto_tsquery placeholder partagé match/rang (le COUNT ne voit que // le WHERE : aucun placeholder ne doit n'exister que dans le ORDER BY). // Même règle pour le pattern LIKE du nom (bonus de rang). tsPh := ph(q) namePh := ph(`%` + escapeLike(q) + `%`) matchAlts := []string{ `r.name ILIKE ` + namePh + ` ESCAPE '\'`, `r.search_tsv @@ plainto_tsquery('french', ` + tsPh + `)`, } // Fact word matches (words >= 3 chars — shorter are French stopwords). // `pat` est partagé entre le match et le bonus du rang : le COUNT ne voit // que le WHERE, aucun placeholder ne doit n'exister que dans le ORDER BY. words := longWords(q, 3, 5) var wordConds, wordCondsHigh []string for _, w := range words { pat := ph(`%` + escapeLike(w) + `%`) wordConds = append(wordConds, `EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.value_text ILIKE `+pat+` ESCAPE '\')`) wordCondsHigh = append(wordCondsHigh, `EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.confidence >= 0.5 AND f.value_text ILIKE `+pat+` ESCAPE '\')`) } if len(wordConds) > 0 { matchAlts = append(matchAlts, `(`+strings.Join(wordConds, " OR ")+`)`) } rankParts := []string{ // Match nom exact : bonus dominant (2.0) — un nom trouvé bat // toujours un fait douteux (0.5), humble par design. `CASE WHEN r.name ILIKE ` + namePh + ` ESCAPE '\' THEN 2.0 ELSE 0 END`, `COALESCE(ts_rank_cd(r.search_tsv, plainto_tsquery('french', ` + tsPh + `)), 0)`, } if len(wordCondsHigh) > 0 { rankParts = append(rankParts, `CASE WHEN (`+strings.Join(wordCondsHigh, " OR ")+`) THEN 1.0 WHEN (`+strings.Join(wordConds, " OR ")+`) THEN 0.5 ELSE 0 END`) } conds := []string{ `r.type = 'file'`, `r.deleted_at IS NULL`, `r.resource_id IN (SELECT id FROM granted)`, `(` + strings.Join(matchAlts, " OR ") + `)`, } // Facets (AND). if facets.Issuer != "" { conds = append(conds, `EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'issuer' AND f.value_text ILIKE `+ph(`%`+escapeLike(facets.Issuer)+`%`)+` ESCAPE '\')`) } if facets.DocType != "" { conds = append(conds, `EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'doc_type' AND f.value_text = `+ph(facets.DocType)+`)`) } if facets.MinAmount != nil { conds = append(conds, `EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'amount' AND f.value_num >= `+ph(*facets.MinAmount)+`)`) } if facets.MaxAmount != nil { conds = append(conds, `EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'amount' AND f.value_num <= `+ph(*facets.MaxAmount)+`)`) } if facets.From != "" || facets.To != "" { from := facets.From if from == "" { from = "0001-01-01" } to := facets.To if to == "" { to = "9999-12-31" } fromMonth, toMonth := from, to if len(fromMonth) >= 7 { fromMonth = fromMonth[:7] } if len(toMonth) >= 7 { toMonth = toMonth[:7] } conds = append(conds, `( EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type IN ('date', 'due_date') AND f.value_date >= `+ph(from)+`::date AND f.value_date <= `+ph(to)+`::date) OR EXISTS (SELECT 1 FROM resource_facts f WHERE f.resource_id = r.resource_id AND f.fact_type = 'period' AND f.value_text >= `+ph(fromMonth)+` AND f.value_text <= `+ph(toMonth)+`) )`) } where = strings.Join(conds, " AND ") order = strings.Join(rankParts, " + ") + " DESC" return where, order, args } // longWords splits q into words of at least minLen runes, capped at max. func longWords(q string, minLen, max int) []string { var out []string seen := map[string]bool{} for _, w := range strings.Fields(q) { if len([]rune(w)) < minLen || seen[w] { continue } seen[w] = true out = append(out, w) if len(out) >= max { break } } return out }