feat: change simple search endpoint params

This commit is contained in:
Miroslav Cvetic 2025-05-19 16:09:24 +02:00
parent 12fc7c8448
commit 5a3e72f0a0
4 changed files with 37 additions and 58 deletions

View File

@ -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

View File

@ -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 {

View File

@ -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)

View File

@ -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)
}