mirror of
https://github.com/foomo/typesense.git
synced 2025-10-16 12:45:37 +00:00
feat: change simple search endpoint params
This commit is contained in:
parent
12fc7c8448
commit
5a3e72f0a0
@ -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
|
||||
|
||||
@ -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 {
|
||||
|
||||
@ -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)
|
||||
|
||||
13
pkg/vo.go
13
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)
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user