Skip to content

Commit 1e3aeda

Browse files
author
Loïc Say
committed
fix(search-account): copy index scope
1 parent f9f9ebb commit 1e3aeda

File tree

2 files changed

+25
-5
lines changed

2 files changed

+25
-5
lines changed

algolia/search/account.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,17 @@ func NewAccount() *Account {
2424
// which belong to different Algolia applications. To perform the same operation
2525
// on indices which belong to the same Algolia application, use Client.CopyIndex
2626
// which is optimized for this use-case.
27-
func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group, error) {
27+
func (a *Account) CopyIndex(src, dst *Index, opts ...string) (*wait.Group, error) {
28+
// Validate scope option
29+
hasScope := len(opts) > 0
30+
if hasScope {
31+
for _, scope := range opts {
32+
if scope != "rules" && scope != "synonyms" && scope != "settings" {
33+
return nil, fmt.Errorf("wrong scope: should be 'rules', 'synonyms'or 'settings'")
34+
}
35+
}
36+
}
37+
2838
if src.GetAppID() == dst.GetAppID() {
2939
return nil, errs.ErrSameAppID
3040
}
@@ -36,7 +46,7 @@ func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group,
3646
g := wait.NewGroup()
3747

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

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

97107
// Copy settings
98-
{
108+
if !hasScope || (hasScope && SliceContains(opts, "settings")) {
99109
settings, err := src.GetSettings()
100110
if err != nil {
101111
return nil, fmt.Errorf("cannot retrieve source index settings: %v", err)
@@ -141,7 +151,7 @@ func (a *Account) CopyIndex(src, dst *Index, opts ...interface{}) (*wait.Group,
141151
}
142152

143153
// Send the last batch
144-
res, err := dst.SaveObjects(objects, opts)
154+
res, err := dst.SaveObjects(objects)
145155
if err != nil {
146156
return nil, fmt.Errorf("error while saving batch of objects: %v", err)
147157
}

algolia/search/utils.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,3 +106,13 @@ func hasObjectID(object interface{}) bool {
106106
_, ok := getObjectID(object)
107107
return ok
108108
}
109+
110+
// Contains check if a slice contains a given string
111+
func SliceContains(s []string, e string) bool {
112+
for _, a := range s {
113+
if a == e {
114+
return true
115+
}
116+
}
117+
return false
118+
}

0 commit comments

Comments
 (0)