mirror of
https://github.com/foomo/typesense.git
synced 2025-10-16 12:45:37 +00:00
feat: support filter by multiple values
This commit is contained in:
parent
50b4b69567
commit
6a1a8e796f
@ -270,7 +270,7 @@ func (b *BaseAPI[indexDocument, returnType]) SimpleSearch(
|
||||
ctx context.Context,
|
||||
index pkgtypesense.IndexID,
|
||||
q string,
|
||||
filterBy map[string]string,
|
||||
filterBy map[string][]string,
|
||||
page, perPage int,
|
||||
sortBy string,
|
||||
) ([]returnType, pkgtypesense.Scores, error) {
|
||||
|
||||
@ -18,7 +18,7 @@ import (
|
||||
// for the typesense search API without any knowledge of the typesense API
|
||||
func buildSearchParams(
|
||||
q string,
|
||||
filterBy map[string]string,
|
||||
filterBy map[string][]string, // Updated to allow multiple values per field
|
||||
page, perPage int,
|
||||
sortBy string,
|
||||
) *api.SearchCollectionParams {
|
||||
@ -36,31 +36,33 @@ func buildSearchParams(
|
||||
return parameters
|
||||
}
|
||||
|
||||
func formatFilterQuery(filterBy map[string]string) string {
|
||||
func formatFilterQuery(filterBy map[string][]string) string {
|
||||
if filterBy == nil {
|
||||
return ""
|
||||
}
|
||||
filterByString := []string{}
|
||||
for key, value := range filterBy {
|
||||
filterByString = append(filterByString, key+":="+value)
|
||||
|
||||
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, ",")))
|
||||
}
|
||||
}
|
||||
return strings.Join(filterByString, "&&")
|
||||
|
||||
return strings.Join(filterClauses, " && ") // AND conditions by default
|
||||
}
|
||||
|
||||
func (b *BaseAPI[indexDocument, returnType]) generateRevisionID() pkgtypesense.RevisionID {
|
||||
return pkgtypesense.RevisionID(time.Now().Format("2006-01-02-15-04")) // "YYYY-MM-DD-HH-MM"
|
||||
}
|
||||
|
||||
func getLatestRevisionID(revisions map[pkgtypesense.IndexID]pkgtypesense.RevisionID) pkgtypesense.RevisionID {
|
||||
var latest pkgtypesense.RevisionID
|
||||
for _, rev := range revisions {
|
||||
if rev > latest {
|
||||
latest = rev
|
||||
}
|
||||
}
|
||||
return latest
|
||||
}
|
||||
|
||||
func formatCollectionName(indexID pkgtypesense.IndexID, revisionID pkgtypesense.RevisionID) string {
|
||||
return fmt.Sprintf("%s-%s", indexID, revisionID)
|
||||
}
|
||||
|
||||
@ -21,7 +21,7 @@ type API[indexDocument any, returnType any] interface {
|
||||
ctx context.Context,
|
||||
index IndexID,
|
||||
q string,
|
||||
filterBy map[string]string,
|
||||
filterBy map[string][]string,
|
||||
page, perPage int,
|
||||
sortBy string,
|
||||
) ([]returnType, Scores, error)
|
||||
|
||||
Loading…
Reference in New Issue
Block a user