V3 S4 : recherche FTS + facettes (serveur + mobile)
Backend : search_tsv french + GIN (000020), SearchDocumentsVisible (nom + TS + faits, rang nom > TS > fait sûr > fait douteux, jamais d'exclusion), facettes issuer/type/montant/période sur GET /files/search (400 si malformée). Tests repo + handlers + migrations. Mobile (Room v21) : FTS4 files_fts + triggers partagés (fresh-install/migration, entité déclarée pour KSP, SQL aligné 21.json), searchDocuments offline (même rang, accents préservés) + UI filtres (émetteur, types chips, montants, période ISO, compteur, effacer). Contrat docs/api-v1.md, V3.md (S4 livré, migrations renumérotées links->000021, token->000022).
This commit is contained in:
@@ -0,0 +1,169 @@
|
||||
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
|
||||
}
|
||||
@@ -0,0 +1,165 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/kazier/backend/dbtest"
|
||||
)
|
||||
|
||||
const searchTestURL = "postgres://kazier:kazier@localhost:5432/kazier_search_test?sslmode=disable"
|
||||
|
||||
func newSearchRepo(t *testing.T) *Repository {
|
||||
t.Helper()
|
||||
return NewRepository(dbtest.OpenTestDatabase(t, searchTestURL))
|
||||
}
|
||||
|
||||
func mustSearchUser(t *testing.T, repo *Repository, username string) string {
|
||||
t.Helper()
|
||||
id, err := repo.Users.Create(username, username, "test-hash", false)
|
||||
if err != nil {
|
||||
t.Fatalf("create user: %v", err)
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func seedDoc(t *testing.T, repo *Repository, userID, name, ocr string, facts []FactParam) string {
|
||||
t.Helper()
|
||||
id := NewID()
|
||||
if err := repo.Resources.InsertFile(userID, id, name, "", 0, nil, nil); err != nil {
|
||||
t.Fatalf("insert %q: %v", name, err)
|
||||
}
|
||||
if ocr != "" {
|
||||
if err := repo.Resources.UpdateOcrText(userID, id, ocr); err != nil {
|
||||
t.Fatalf("ocr %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
if len(facts) > 0 {
|
||||
if err := repo.Resources.UpsertAutoFacts(id, facts); err != nil {
|
||||
t.Fatalf("facts %q: %v", name, err)
|
||||
}
|
||||
}
|
||||
return id
|
||||
}
|
||||
|
||||
func f64(v float64) *float64 { return &v }
|
||||
func fstr(v string) *string { return &v }
|
||||
|
||||
func searchIDs(t *testing.T, repo *Repository, userID, q string, facets SearchFacets) []string {
|
||||
t.Helper()
|
||||
rows, _, err := repo.Resources.SearchDocumentsVisible(userID, q, facets, 50, 0)
|
||||
if err != nil {
|
||||
t.Fatalf("search %q: %v", q, err)
|
||||
}
|
||||
ids := make([]string, 0, len(rows))
|
||||
for _, r := range rows {
|
||||
ids = append(ids, r.ID)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func containsID(ids []string, want string) bool {
|
||||
for _, id := range ids {
|
||||
if id == want {
|
||||
return true
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func TestSearchDocumentsFTS(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-fts")
|
||||
|
||||
// Nom opaque : trouvable uniquement via l'OCR (TS français).
|
||||
scan := seedDoc(t, repo, userID, "scan_042.pdf",
|
||||
"Attestation d'assurance MMA pour l'année 2026", nil)
|
||||
plain := seedDoc(t, repo, userID, "assurance-vie.txt", "", nil)
|
||||
|
||||
ids := searchIDs(t, repo, userID, "assurance", SearchFacets{})
|
||||
if !containsID(ids, scan) {
|
||||
t.Errorf("scan_042.pdf introuvable via OCR: %v", ids)
|
||||
}
|
||||
if !containsID(ids, plain) {
|
||||
t.Errorf("assurance-vie.txt introuvable via nom: %v", ids)
|
||||
}
|
||||
|
||||
// Stemming français : "factures" matche "facture".
|
||||
fact := seedDoc(t, repo, userID, "doc.pdf", "Votre facture de mars", nil)
|
||||
ids = searchIDs(t, repo, userID, "factures", SearchFacets{})
|
||||
if !containsID(ids, fact) {
|
||||
t.Errorf("stemming FR manquant: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsFacets(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-facets")
|
||||
|
||||
edf := seedDoc(t, repo, userID, "edf.pdf", "Facture EDF", []FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "84.20 €", ValueNum: f64(84.20), Confidence: 0.9},
|
||||
{Type: "due_date", ValueText: "2026-04-12", ValueDate: fstr("2026-04-12"), Confidence: 0.8},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
})
|
||||
engie := seedDoc(t, repo, userID, "engie.pdf", "Facture Engie", []FactParam{
|
||||
{Type: "issuer", ValueText: "ENGIE", Confidence: 0.9},
|
||||
{Type: "amount", ValueText: "120.00 €", ValueNum: f64(120), Confidence: 0.9},
|
||||
{Type: "doc_type", ValueText: "facture", Confidence: 0.8},
|
||||
})
|
||||
|
||||
ids := searchIDs(t, repo, userID, "facture", SearchFacets{Issuer: "edf"})
|
||||
if !containsID(ids, edf) || containsID(ids, engie) {
|
||||
t.Errorf("facette issuer: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{MinAmount: f64(100)})
|
||||
if !containsID(ids, engie) || containsID(ids, edf) {
|
||||
t.Errorf("facette min_amount: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{DocType: "facture"})
|
||||
if !containsID(ids, edf) || !containsID(ids, engie) {
|
||||
t.Errorf("facette doc_type: %v", ids)
|
||||
}
|
||||
|
||||
ids = searchIDs(t, repo, userID, "facture", SearchFacets{From: "2026-04-01", To: "2026-04-30"})
|
||||
if !containsID(ids, edf) || containsID(ids, engie) {
|
||||
t.Errorf("facette période: %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsLowConfidenceDemotes(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-humble")
|
||||
|
||||
// Fait douteux (0.3) : trouvé quand même (jamais exclu), mais après le
|
||||
// match franc (nom) — humble par design.
|
||||
sure := seedDoc(t, repo, userID, "EDF-contrat.pdf", "", nil)
|
||||
doubt := seedDoc(t, repo, userID, "scan_x.pdf", "courrier", []FactParam{
|
||||
{Type: "issuer", ValueText: "EDF", Confidence: 0.3},
|
||||
})
|
||||
|
||||
ids := searchIDs(t, repo, userID, "EDF", SearchFacets{})
|
||||
if !containsID(ids, doubt) {
|
||||
t.Fatalf("fait douteux exclu (interdit): %v", ids)
|
||||
}
|
||||
if len(ids) < 2 || ids[0] != sure || ids[1] != doubt {
|
||||
t.Errorf("ordre de rang inattendu (franc avant douteux): %v", ids)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSearchDocumentsTrashedExcluded(t *testing.T) {
|
||||
repo := newSearchRepo(t)
|
||||
userID := mustSearchUser(t, repo, "search-trash")
|
||||
id := seedDoc(t, repo, userID, "poubelle.pdf", "contenu unique xyz", nil)
|
||||
|
||||
if ids := searchIDs(t, repo, userID, "xyz", SearchFacets{}); !containsID(ids, id) {
|
||||
t.Fatalf("introuvable avant trash: %v", ids)
|
||||
}
|
||||
if err := repo.Resources.TrashResource(userID, id); err != nil {
|
||||
t.Fatalf("trash: %v", err)
|
||||
}
|
||||
if ids := searchIDs(t, repo, userID, "xyz", SearchFacets{}); containsID(ids, id) {
|
||||
t.Errorf("trashé encore visible: %v", ids)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user