From 5a3e72f0a0c27959bc39c06e17d189a8ed3fab77 Mon Sep 17 00:00:00 2001 From: Miroslav Cvetic Date: Mon, 19 May 2025 16:09:24 +0200 Subject: [PATCH] feat: change simple search endpoint params --- pkg/api/api.go | 13 ++++------- pkg/api/utils.go | 59 ++++++++++++++++-------------------------------- pkg/interface.go | 10 +------- pkg/vo.go | 13 ++++++++++- 4 files changed, 37 insertions(+), 58 deletions(-) diff --git a/pkg/api/api.go b/pkg/api/api.go index 7266028..2abe97f 100644 --- a/pkg/api/api.go +++ b/pkg/api/api.go @@ -266,19 +266,14 @@ func (b *BaseAPI[indexDocument, returnType]) RevertRevision(ctx context.Context, return nil } -// SimpleSearch will perform a search operation on the given index -// it will return the documents and the scores +// SimpleSearch will perform a search operation on the given index using basic SearchParameters input func (b *BaseAPI[indexDocument, returnType]) SimpleSearch( ctx context.Context, index pkgx.IndexID, - q string, - filterBy map[string][]string, - page, perPage int, - sortBy string, - queryBy string, + parameters *pkgx.SearchParameters, ) ([]returnType, pkgx.Scores, int, error) { - parameters := buildSearchParams(q, filterBy, page, perPage, sortBy, queryBy) - return b.ExpertSearch(ctx, index, parameters) + searchParams := buildSearchParams(parameters) + return b.ExpertSearch(ctx, index, searchParams) } // ExpertSearch performs a search operation on the given index diff --git a/pkg/api/utils.go b/pkg/api/utils.go index 28c7d33..2bb73a5 100644 --- a/pkg/api/utils.go +++ b/pkg/api/utils.go @@ -13,54 +13,35 @@ import ( "go.uber.org/zap" ) +const defaultSearchPresetName = "default" + // buildSearchParams will return the search collection parameters -// this is meant as a utility function to create the search collection parameters -// for the typesense search API without any knowledge of the typesense API func buildSearchParams( - q string, - filterBy map[string][]string, - page, perPage int, - sortBy string, - queryBy string, + params *pkgx.SearchParameters, ) *api.SearchCollectionParams { - parameters := &api.SearchCollectionParams{} - parameters.Q = pointer.String(q) - if filterByString := formatFilterQuery(filterBy); filterByString != "" { - parameters.FilterBy = pointer.String(filterByString) - } - parameters.Page = pointer.Int(page) - parameters.PerPage = pointer.Int(perPage) - if sortBy != "" { - parameters.SortBy = pointer.String(sortBy) - } - if queryBy != "" { - parameters.QueryBy = pointer.String(queryBy) + if params.Page < 1 { + params.Page = 1 } - return parameters -} - -func formatFilterQuery(filterBy map[string][]string) string { - if filterBy == nil { - return "" + searchParams := &api.SearchCollectionParams{ + Page: pointer.Int(params.Page), } - var filterClauses []string - for key, values := range filterBy { - if len(values) == 1 { - // Single value → Use `:=` operator - filterClauses = append(filterClauses, fmt.Sprintf("%s:=\"%s\"", key, values[0])) - } else { - // Multiple values → Use `["val1","val2"]` array syntax - formattedValues := []string{} - for _, v := range values { - formattedValues = append(formattedValues, fmt.Sprintf("\"%s\"", v)) - } - filterClauses = append(filterClauses, fmt.Sprintf("%s:[%s]", key, strings.Join(formattedValues, ","))) - } + if params.PresetName != "" { + searchParams.Preset = pointer.String(params.PresetName) + } else { + searchParams.Preset = pointer.String(defaultSearchPresetName) } - return strings.Join(filterClauses, " && ") + if params.Query != "" { + searchParams.Q = pointer.String(params.Query) + } + + if params.Modify != nil { + params.Modify(searchParams) + } + + return searchParams } func (b *BaseAPI[indexDocument, returnType]) generateRevisionID() pkgx.RevisionID { diff --git a/pkg/interface.go b/pkg/interface.go index 38b7a72..572c3fb 100644 --- a/pkg/interface.go +++ b/pkg/interface.go @@ -17,15 +17,7 @@ type API[indexDocument any, returnType any] interface { Initialize(ctx context.Context) (RevisionID, error) // perform a search operation on the given index - SimpleSearch( - ctx context.Context, - index IndexID, - q string, - filterBy map[string][]string, - page, perPage int, - sortBy string, - queryBy string, - ) ([]returnType, Scores, int, error) + SimpleSearch(ctx context.Context, index IndexID, parameters *SearchParameters) ([]returnType, Scores, int, error) ExpertSearch(ctx context.Context, index IndexID, parameters *api.SearchCollectionParams) ([]returnType, Scores, int, error) Healthz(ctx context.Context) error Indices() ([]IndexID, error) diff --git a/pkg/vo.go b/pkg/vo.go index b7acbb2..a90027e 100644 --- a/pkg/vo.go +++ b/pkg/vo.go @@ -1,6 +1,10 @@ package typesense -import "context" +import ( + "context" + + "github.com/typesense/typesense-go/v3/typesense/api" +) type RevisionID string type Query string @@ -26,3 +30,10 @@ type DocumentInfo struct { DocumentType DocumentType DocumentID DocumentID } + +type SearchParameters struct { + Query string + Page int + PresetName string + Modify func(params *api.SearchCollectionParams) +}