FTS5-Suche

This commit is contained in:
Simon Martens
2025-02-22 19:08:36 +01:00
parent 29576ec7a0
commit 3d54725283
28 changed files with 795 additions and 77 deletions

View File

@@ -8,6 +8,8 @@ import (
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers/datatypes"
"github.com/pocketbase/dbx"
"github.com/pocketbase/pocketbase/core"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
const (
@@ -94,6 +96,52 @@ var CONTENTS_FTS5_FIELDS = []string{
COMMENT_FIELD,
}
var ErrInvalidQuery = errors.New("invalid input into the search function")
func NormalizeQuery(query string) []string {
query = datatypes.NormalizeString(query)
query = datatypes.DeleteTags(query)
query = datatypes.RemovePunctuation(query)
query = cases.Lower(language.German).String(query)
// TODO: how to normalize, which unicode normalization to use?
split := strings.Split(query, " ")
res := []string{}
for _, s := range split {
if len(s) > 2 {
res = append(res, s)
}
}
return res
}
func FTS5Search(app core.App, table string, mapfq ...FTS5QueryRequest) ([]*FTS5IDQueryResult, error) {
if mapfq == nil || len(mapfq) == 0 || table == "" {
return nil, ErrInvalidQuery
}
q := NewFTS5Query().From(table).SelectID()
for _, v := range mapfq {
for _, que := range v.Query {
q.AndMatch(v.Fields, que)
}
}
querystring := q.Query()
if querystring == "" {
return nil, ErrInvalidQuery
}
res := []*FTS5IDQueryResult{}
err := app.DB().NewQuery(querystring).All(&res)
if err != nil {
return nil, err
}
return res, nil
}
func CreateFTS5TableQuery(tablename string, fields ...string) string {
if len(fields) == 0 {
return ""

View File

@@ -6,6 +6,11 @@ import (
"github.com/Theodor-Springmann-Stiftung/musenalm/helpers/datatypes"
)
type FTS5QueryRequest struct {
Fields []string
Query []string
}
type FTS5IDQueryResult struct {
ID string `db:"id"`
}

View File

@@ -51,7 +51,15 @@ func BasicSearchSeries(app core.App, query string) ([]*Series, []*Series, error)
}
// INFO: Needing to differentiate matches
altids, err := FTS5SearchSeries(app, query)
querysplit := NormalizeQuery(query)
if len(querysplit) == 0 {
return series, []*Series{}, nil
}
altids, err := FTS5Search(app, SERIES_TABLE, FTS5QueryRequest{
Fields: []string{SERIES_TITLE_FIELD, ANNOTATION_FIELD, REFERENCES_FIELD},
Query: querysplit,
})
if err != nil {
return nil, nil, err
}