package knowledge import ( "sort" "strings" "unicode" "yetano.dev/yetano/core/solutions/ref" ) // Hit is one search result. type Hit struct { Ref string `json:"ref"` Kind string `json:"kind"` Title string `json:"title"` Summary string `json:"summary,omitempty"` Score float64 `json:"score"` Status string `json:"status,omitempty"` } // Group collects the hits of one kind (SEARCH-01). type Group struct { Kind string `json:"kind"` Title string `json:"title"` Hits []Hit `json:"hits"` } // SearchOptions narrow a search. type SearchOptions struct { Kinds []string // only these kinds (solution, task, package, manual, ...) Limit int // per group; 0 = 10 Locale string Package string } // Group order: use cases first (SEARCH-04). var groupOrder = []struct{ kind, title string }{ {ref.Solution, "Solutions"}, {ref.Task, "Tasks"}, {ref.Example, "Examples"}, {ref.Package, "Packages"}, {ref.Module, "Modules"}, {ref.Capability, "Capabilities"}, {ref.Manual, "Manuals"}, {"section", "Manual sections"}, {ref.Microsite, "Microsites"}, {ref.BigPicture, "Big Picture"}, {ref.Profile, "Profiles"}, } var stop = map[string]bool{"i": true, "a": true, "an": true, "the": true, "to": true, "want": true, "my": true, "own": true, "with": true, "for": true, "of": true, "and": true, "run": false, "ich": true, "will": true, "einen": true, "ein": true, "mit": true, "möchte": true} // Tokens splits text into lower-case search words. func Tokens(s string) []string { f := strings.FieldsFunc(strings.ToLower(s), func(r rune) bool { return !unicode.IsLetter(r) && !unicode.IsDigit(r) && r != '.' }) var out []string for _, w := range f { w = strings.Trim(w, ".") if w != "" && !stop[w] { out = append(out, w) } } return out } // Search runs a deterministic full-text search over the index: titles, // IDs, tags and synonyms weigh most, then summaries, then body text. // No embeddings and no network (SEARCH-05). func (ix *Index) Search(q string, opt SearchOptions) []Group { words := Tokens(q) if len(words) == 0 { return nil } limit := opt.Limit if limit <= 0 { limit = 10 } allowed := map[string]bool{} for _, k := range opt.Kinds { allowed[k] = true } byKind := map[string][]Hit{} for _, e := range ix.Entries { if len(allowed) > 0 && !allowed[e.Kind] { continue } if opt.Package != "" && e.Package != opt.Package { continue } title := e.Title summary := e.Summary if opt.Locale != "" && e.Translations != nil { if m, ok := e.Translations[opt.Locale]; ok { title = firstNonEmpty(m["title"], title) summary = firstNonEmpty(m["summary"], summary) } } fields := []struct { text string weight float64 }{ {title + " " + e.Title, 6}, {e.Ref, 4}, {strings.Join(e.Tags, " ") + " " + strings.Join(e.Synonyms, " "), 5}, {strings.Join(e.Capabilities, " ") + " " + e.Package, 3}, {summary + " " + e.Summary, 2}, {e.Body, 0.5}, } score := 0.0 matched := 0 phrase := strings.ToLower(strings.Join(words, " ")) for _, w := range words { best := 0.0 for _, f := range fields { lt := strings.ToLower(f.text) if strings.Contains(lt, w) { s := f.weight if hasWord(lt, w) { s *= 1.5 } if s > best { best = s } } } if best > 0 { matched++ } score += best } if matched == 0 { continue } score *= float64(matched) / float64(len(words)) if strings.Contains(strings.ToLower(title+" "+strings.Join(e.Synonyms, " ")), phrase) && len(words) > 1 { score += 5 } switch e.Kind { case ref.Solution, ref.Task: score *= 1.5 case "section": score *= 0.6 } byKind[e.Kind] = append(byKind[e.Kind], Hit{Ref: e.Ref, Kind: e.Kind, Title: title, Summary: summary, Score: round(score), Status: e.Status}) } var out []Group for _, g := range groupOrder { hits := byKind[g.kind] if len(hits) == 0 { continue } sort.SliceStable(hits, func(i, j int) bool { if hits[i].Score != hits[j].Score { return hits[i].Score > hits[j].Score } return hits[i].Ref < hits[j].Ref }) if len(hits) > limit { hits = hits[:limit] } out = append(out, Group{Kind: g.kind, Title: g.title, Hits: hits}) } // groups with the best hit first, but solutions and tasks keep the lead sort.SliceStable(out, func(i, j int) bool { pi, pj := lead(out[i].Kind), lead(out[j].Kind) if pi != pj { return pi > pj } return out[i].Hits[0].Score > out[j].Hits[0].Score }) return out } func lead(kind string) int { if kind == ref.Solution || kind == ref.Task { return 1 } return 0 } func hasWord(text, w string) bool { for _, t := range Tokens(text) { if t == w { return true } } return false } func round(f float64) float64 { return float64(int(f*100+0.5)) / 100 }