Skip to content

Commit 8b1dd19

Browse files
Merge pull request #45 from algolia/new_synonyms
New synonym end points
2 parents 0290cf8 + edd92a9 commit 8b1dd19

File tree

2 files changed

+116
-1
lines changed

2 files changed

+116
-1
lines changed

algoliasearch/index.py

Lines changed: 94 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -543,6 +543,97 @@ def browse_all(self, params=None):
543543
"""
544544
return IndexIterator(self, params=params)
545545

546+
def save_synonym(self, content, object_id, forward_to_slaves=False):
547+
"""
548+
Add a synonym in this index.
549+
550+
@param content contains the synonyms set to add to the index.
551+
The object is represented by an associative array
552+
@param object_id unique identifier for the new synonym.
553+
If the identifier already exists, the old synonym is replaced
554+
@param forward_to_slaves (optional) should the changes be forwarded to
555+
slave indexes
556+
"""
557+
path = '/synonyms/%s' % safe(object_id)
558+
params = {'forwardToSlaves': forward_to_slaves}
559+
return self._perform_request(self.write_hosts, path, 'PUT',
560+
body=content, params=params)
561+
562+
def batch_synonyms(self, synonyms, forward_to_slaves=False,
563+
replace_existing_synonyms=False):
564+
"""
565+
Add several synonyms in this index.
566+
567+
@param synonyms array of synonyms to add
568+
@param forward_to_slaves (optional) should the changes be forwarded to
569+
slave indexes
570+
@param replace_existing_synonyms (optional) should the index be cleared
571+
of existing synonyms
572+
"""
573+
params = {
574+
'forwardToSlaves': forward_to_slaves,
575+
'replaceExistingSynonyms': replace_existing_synonyms
576+
}
577+
578+
return self._perform_request(self.write_hosts, '/synonyms/batch',
579+
'POST', body=synonyms, params=params)
580+
581+
def get_synonym(self, object_id):
582+
"""
583+
Get a synonym from this index.
584+
585+
@param object_id unique identifier of the synonym to retrieve
586+
"""
587+
path = '/synonyms/%s' % safe(object_id)
588+
return self._perform_request(self.read_hosts, path, 'GET')
589+
590+
def delete_synonym(self, object_id, forward_to_slaves=False):
591+
"""
592+
Delete a synonym from the index.
593+
594+
@param object_id the unique identifier of the synonyms set to delete
595+
@param forward_to_slaves (optional) should the changes be forwarded to
596+
slave indexes
597+
"""
598+
path = '/synonyms/%s' % safe(object_id)
599+
params = {'forwardToSlaves': forward_to_slaves}
600+
return self._perform_request(self.write_hosts, path, 'DELETE',
601+
params=params)
602+
603+
def clear_synonyms(self, forward_to_slaves=False):
604+
"""
605+
Delete all synonyms from the index.
606+
607+
@param forward_to_slaves (optional) should the changes be forwarded to
608+
slave indexes
609+
"""
610+
path = '/synonyms/clear'
611+
params = {'forwardToSlaves': forward_to_slaves}
612+
return self._perform_request(self.write_hosts, path, 'POST',
613+
params=params)
614+
615+
def search_synonyms(self, query, types=[], page=0, hits_per_page=100):
616+
"""
617+
Search for synonyms from this index.
618+
619+
@param query the full text query
620+
@param types (optional) the types of the synonyms to search for.
621+
@param page (optional integer) the page to fetch
622+
@param hits_per_page (optional integer) the number of hits per page
623+
"""
624+
if isinstance(types, str):
625+
types = [] if len(types) == 0 else [types]
626+
627+
body = {
628+
'query': query,
629+
'type': ','.join(types),
630+
'page': page,
631+
'hitsPerPage': hits_per_page
632+
}
633+
634+
return self._perform_request(self.read_hosts, '/synonyms/search',
635+
'POST', body=body, is_search=True)
636+
546637
@deprecated
547638
def waitTask(self, task_id, time_before_retry=100):
548639
return self.wait_task(task_id, time_before_retry)
@@ -569,7 +660,9 @@ def getSettings(self):
569660

570661
def get_settings(self):
571662
"""Get settings of this index."""
572-
return self._perform_request(self.read_hosts, '/settings', 'GET')
663+
params = {'getVersion': 2}
664+
return self._perform_request(self.read_hosts, '/settings', 'GET',
665+
params=params)
573666

574667
@deprecated
575668
def clearIndex(self):

tests/test_index.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,28 @@ def test_encode_datetime(self):
111111
res = self.index.get_object(task['objectID'])
112112
self.assertEqual(res['now'], time.mktime(value.timetuple()))
113113

114+
def test_synonyms(self):
115+
task = self.index.add_object({'name': '589 Howard St., San Francisco'})
116+
task = self.index.batch_synonyms([
117+
{'objectID': 'city', 'type': 'synonym',
118+
'synonyms': ['San Francisco', 'SF']},
119+
{'objectID': 'street', 'type': 'altCorrection1',
120+
'word': 'Street', 'corrections': ['St']}
121+
])
122+
self.index.wait_task(task['taskID'])
123+
task = self.index.get_synonym("city")
124+
self.assertEqual('city', task['objectID'])
125+
task = self.index.search('Howard Street SF')
126+
self.assertEqual(1, int(task['nbHits']))
127+
task = self.index.delete_synonym('street')
128+
self.index.waitTask(task['taskID'])
129+
task = self.index.search_synonyms('', ['synonym'], 0, 5)
130+
self.assertEqual(1, int(task['nbHits']))
131+
task = self.index.clear_synonyms()
132+
self.index.wait_task(task['taskID'])
133+
task = self.index.search_synonyms('', hits_per_page=5)
134+
self.assertEqual(0, int(task['nbHits']))
135+
114136

115137
class IndexWithReadOnlyDataTest(IndexTest):
116138
"""Tests that use one index with initial data (read only)."""

0 commit comments

Comments
 (0)