Skip to content

Commit

Permalink
refactor: add clearExistingSynonyms option and its handling in saveSy…
Browse files Browse the repository at this point in the history
…nonyms method
  • Loading branch information
VladislavFitz committed Nov 24, 2020
1 parent 14ee0eb commit 3958984
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 2 deletions.
3 changes: 2 additions & 1 deletion algolia/internal/gen/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ var options = []Option{
{"enabled", Other, true, ""}, // Used to enable the rule by default

// Personalization
{"enablePersonalization", Search|Settings, false, ""},
{"enablePersonalization", Search | Settings, false, ""},
{"personalizationImpact", Search, 100, ""},
{"userToken", Search, "", ""},
{"enableReRanking", Search | Settings, true, ""},
Expand Down Expand Up @@ -155,6 +155,7 @@ var options = []Option{
{"query", Other, "", ""},
{"autoGenerateObjectIDIfNotExist", Other, false, ""},
{"clearExistingRules", Other, false, ""},
{"clearExistingSynonyms", Other, false, ""},
{"replaceExistingSynonyms", Other, false, ""},
{"type", Other, nil, ""},
{"createIfNotExists", Other, true, ""},
Expand Down
18 changes: 18 additions & 0 deletions algolia/internal/opt/clear_existing_synonyms.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 41 additions & 0 deletions algolia/internal/opt/clear_existing_synonyms_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

60 changes: 60 additions & 0 deletions algolia/opt/clear_existing_synonyms.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions algolia/opt/option_getters_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion algolia/search/index_synonyms.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ func (i *Index) SaveSynonym(synonym Synonym, opts ...interface{}) (res UpdateTas
// Unlike SaveObjects, this method does not batch the given synonyms i.e. all synonyms
// are sent in a single call.
func (i *Index) SaveSynonyms(synonyms []Synonym, opts ...interface{}) (res UpdateTaskRes, err error) {
if replaceExistingSynonyms := iopt.ExtractReplaceExistingSynonyms(opts...); replaceExistingSynonyms != nil {

if clearExistingSynonyms := iopt.ExtractClearExistingSynonyms(opts...); clearExistingSynonyms != nil {
opts = opt.InsertExtraURLParam(opts, "replaceExistingSynonyms", clearExistingSynonyms.Get())
} else if replaceExistingSynonyms := iopt.ExtractReplaceExistingSynonyms(opts...); replaceExistingSynonyms != nil {
// if clearExistingSynonyms parameter is missing, attempt to parse legacy replaceExistingSynonyms option
opts = opt.InsertExtraURLParam(opts, "replaceExistingSynonyms", replaceExistingSynonyms.Get())
}

path := i.path("/synonyms/batch")
err = i.transport.Request(&res, http.MethodPost, path, synonyms, call.Write, opts...)
res.wait = i.WaitTask
Expand Down

0 comments on commit 3958984

Please sign in to comment.