Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(search-account): copy index scope #706

Open
wants to merge 1 commit into
base: v3
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions algolia/search/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,17 @@ func NewAccount() *Account {
// which belong to different Algolia applications. To perform the same operation
// on indices which belong to the same Algolia application, use Client.CopyIndex
// which is optimized for this use-case.
func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group, error) {
func (a *Account) CopyIndex(src, dst *Index, opts ...string) (*wait.Group, error) {
// Validate scope option
hasScope := len(opts) > 0
if hasScope {
for _, scope := range opts {
if scope != "rules" && scope != "synonyms" && scope != "settings" {
return nil, fmt.Errorf("wrong scope: should be 'rules', 'synonyms'or 'settings'")
}
}
}

if src.GetAppID() == dst.GetAppID() {
return nil, errs.ErrSameAppID
}
Expand All @@ -36,7 +46,7 @@ func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group,
g := wait.NewGroup()

// Copy synonyms
{
if !hasScope || SliceContains(opts, "synonyms") {
it, err := src.BrowseSynonyms()
if err != nil {
return nil, fmt.Errorf("cannot browse source index synonyms: %v", err)
Expand Down Expand Up @@ -66,7 +76,7 @@ func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group,
}

// Copy rules
{
if !hasScope || SliceContains(opts, "rules") {
it, err := src.BrowseRules()
if err != nil {
return nil, fmt.Errorf("cannot browse source index rules: %v", err)
Expand Down Expand Up @@ -95,7 +105,7 @@ func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group,
}

// Copy settings
{
if !hasScope || SliceContains(opts, "settings") {
settings, err := src.GetSettings()
if err != nil {
return nil, fmt.Errorf("cannot retrieve source index settings: %v", err)
Expand Down Expand Up @@ -141,7 +151,7 @@ func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group,
}

// Send the last batch
res, err := dst.SaveObjects(objects, opts)
res, err := dst.SaveObjects(objects)
if err != nil {
return nil, fmt.Errorf("error while saving batch of objects: %v", err)
}
Expand Down
10 changes: 10 additions & 0 deletions algolia/search/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,13 @@ func hasObjectID(object interface{}) bool {
_, ok := getObjectID(object)
return ok
}

// Contains check if a slice contains a given string
func SliceContains(s []string, e string) bool {
for _, a := range s {
if a == e {
return true
}
}
return false
}