Skip to content
Merged
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

* Adds `copy_settings`, `copy_synonyms` and `copy_rules` methods on client - PR [#389](https://github.com/algolia/algoliasearch-client-python/pull/389)

* Adds `replace_all_rules` and `replace_all_synonyms` methods on client - PR [#390](https://github.com/algolia/algoliasearch-client-python/pull/390)

### 1.17.0 - 2018-06-19

* Introduce AB Testing feature - PR [#408](https://github.com/algolia/algoliasearch-client-php/pull/#408)
Expand Down
22 changes: 22 additions & 0 deletions algoliasearch/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,6 +672,17 @@ def batch_synonyms(self, synonyms, forward_to_slaves=False,

return self._req(False, '/synonyms/batch', 'POST', request_options, params, synonyms)

def replace_all_synonyms(self, synonyms, request_options=None):
"""
Replace all synonyms in the index.
@param synonyms the synonyms to upload as a list of python dictionary.
"""
params = {
'replaceExistingSynonyms': True
}

return self._req(False, '/synonyms/batch', 'POST', request_options, params, data=synonyms)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Did you not use batch_synonyms on purpose because of the requestOptions?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@julienbourdeau Because all the extra works of setting the params of the batch_synonyms. Is just more simple call one line.


def get_synonym(self, object_id, request_options=None):
"""
Get a synonym from this index.
Expand Down Expand Up @@ -1057,6 +1068,17 @@ def save_rule(self, rule, forward_to_replicas=False, request_options=None):
params = {'forwardToReplicas': forward_to_replicas}
return self._req(False, '/rules/%s' % str(rule['objectID']), 'PUT', request_options, params, rule)

def replace_all_rules(self, rules, request_options=None):
"""
Replace all rules in the index.
@param rules the rules to upload as a list of python dictionary.
"""
params = {
'clearExistingRules': True
}

return self._req(False, '/rules/batch', 'POST', request_options, params, data=rules)

def batch_rules(self, rules, forward_to_replicas=False, clear_existing_rules=False, request_options=None):
"""
Save a batch of new rules
Expand Down
8 changes: 8 additions & 0 deletions tests/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,14 @@ def __getattr__(self, name):
return getattr(self._index, name)


def synonym_stub(objid='my-synonym'):
return {
'objectID': objid,
'type': 'synonym',
'synonyms': ['San Francisco', 'SF']
}


def rule_stub(objid='my-rule'):
return {
'objectID': objid,
Expand Down
50 changes: 49 additions & 1 deletion tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from algoliasearch.client import RequestOptions, MAX_API_KEY_LENGTH
from algoliasearch.helpers import AlgoliaException

from .helpers import Factory, rule_stub
from .helpers import Factory, rule_stub, synonym_stub


def test_add_object(index):
Expand Down Expand Up @@ -113,6 +113,30 @@ def test_synonyms(index):
assert int(task['nbHits']) == 0


def test_replace_all_synonyms(index):
synonym = synonym_stub('one')
synonym2 = synonym_stub('two')

res = index.replace_all_synonyms([synonym, synonym2])
index.wait_task(res['taskID'])

rules = index.search_synonyms('')
assert rules['nbHits'] == 2
assert index.get_synonym('one') == synonym
assert index.get_synonym('two') == synonym2

synonym3 = synonym_stub('three')
synonym4 = synonym_stub('four')

res = index.replace_all_synonyms([synonym3, synonym4])
index.wait_task(res['taskID'])

rules = index.search_synonyms('')
assert rules['nbHits'] == 2
assert index.get_synonym('three') == synonym3
assert index.get_synonym('four') == synonym4


def test_iter_synonyms(index):
synonyms = [{
'objectID': 'city',
Expand Down Expand Up @@ -268,6 +292,30 @@ def test_batch_and_clear_rules(index):
assert rules['nbHits'] == 0


def test_replace_all_rules(index):
rule = rule_stub('one')
rule2 = rule_stub('two')

res = index.replace_all_rules([rule, rule2])
index.wait_task(res['taskID'])

rules = index.search_rules()
assert rules['nbHits'] == 2
assert index.read_rule('one') == rule
assert index.read_rule('two') == rule2

rule3 = rule_stub('three')
rule4 = rule_stub('four')

res = index.replace_all_rules([rule3, rule4])
index.wait_task(res['taskID'])

rules = index.search_rules()
assert rules['nbHits'] == 2
assert index.read_rule('three') == rule3
assert index.read_rule('four') == rule4


def test_batch_and_clear_existing(index):
rule = rule_stub()
rule2 = rule_stub('my-second-rule')
Expand Down