Skip to content

Commit 96c0ff0

Browse files
Merge pull request #319 from algolia/feat/iter_synonyms
feat/iter_synonyms
2 parents 4af2934 + 5ff29ab commit 96c0ff0

File tree

8 files changed

+46
-51
lines changed

8 files changed

+46
-51
lines changed

algoliasearch/index.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -70,37 +70,6 @@ def _load_next_page(self):
7070
self.pos = 0
7171
self.cursor = self.answer.get('cursor', None)
7272

73-
class SynonymIterator:
74-
"""Iterator on the synonyms of an index"""
75-
76-
def __init__(self, index, hits_per_page=1000):
77-
self.index = index
78-
self.hits_per_page = hits_per_page
79-
self.page = 0
80-
81-
def __iter__(self):
82-
self._load_next_page()
83-
return self
84-
85-
def __next__(self):
86-
return self.next()
87-
88-
def next(self):
89-
if self.pos >= len(self.response['hits']):
90-
self._load_next_page()
91-
if self.pos < len(self.response['hits']):
92-
result = self.response['hits'][self.pos]
93-
self.pos += 1
94-
# Remove highlighting.
95-
if '_highlightResult' in result: del result['_highlightResult']
96-
return result
97-
else:
98-
raise StopIteration
99-
100-
def _load_next_page(self):
101-
self.response = self.index.search_synonyms('', page=self.page, hits_per_page=self.hits_per_page)
102-
self.page += 1
103-
self.pos = 0
10473

10574
class RuleIterator:
10675
"""Iterator on the rules of an index"""
@@ -714,6 +683,20 @@ def search_synonyms(self, query, types=[], page=0, hits_per_page=100, request_op
714683

715684
return self._req(True, '/synonyms/search', 'POST', request_options, data=data)
716685

686+
def iter_synonyms(self, hits_per_page=1000):
687+
page = 0
688+
response = self.search_synonyms('', page=page, hits_per_page=hits_per_page)
689+
690+
while response['hits']:
691+
for hit in response['hits']:
692+
if '_highlightResult' in hit:
693+
del hit['_highlightResult']
694+
695+
yield hit
696+
697+
page += 1
698+
response = self.search_synonyms('', page=page, hits_per_page=hits_per_page)
699+
717700
@deprecated
718701
def waitTask(self, task_id, time_before_retry=100):
719702
return self.wait_task(task_id, time_before_retry)

tests/__init__.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import sys
2+
3+
# Use local algoliasearch package.
4+
sys.path.append('..')
5+

tests/conftest.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22

3-
from helpers import Factory
4-
from helpers import create_client, create_index, IndexWithData
3+
from .helpers import Factory
4+
from .helpers import create_client, create_index, IndexWithData
55

66

77
@pytest.fixture

tests/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ def check_credentials():
1717

1818
for credential in credentials:
1919
if credential not in os.environ:
20-
print('environement variable {} not defined')
20+
print('environement variable {} not defined'.format(credential))
2121
assert False
2222

2323

tests/test_client.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44
from algoliasearch.client import RequestOptions, MAX_API_KEY_LENGTH, Client
55
from algoliasearch.helpers import AlgoliaException
6-
from fake_session import FakeSession
7-
from helpers import Factory, check_credentials
6+
from .fake_session import FakeSession
7+
from .helpers import Factory, check_credentials
88

99

1010
def test_request_options(client):

tests/test_compat.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from helpers import wait_key, wait_missing_key
1+
from .helpers import wait_key, wait_missing_key
22

33
def test_addObject(index):
44
task = index.addObject({'name': 'Paris'}, 'a')

tests/test_index.py

Lines changed: 20 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from algoliasearch.client import MAX_API_KEY_LENGTH
88
from algoliasearch.helpers import AlgoliaException
99

10-
from helpers import Factory, rule_stub
10+
from .helpers import Factory, rule_stub
1111

1212

1313
def test_add_object(index):
@@ -113,18 +113,25 @@ def test_synonyms(index):
113113
assert int(task['nbHits']) == 0
114114

115115

116-
# def tests_synonym_iterator(index):
117-
# synonyms = [
118-
# {'objectID': 'city', 'type': 'synonym',
119-
# 'synonyms': ['San Francisco', 'SF']},
120-
# {'objectID': 'street', 'type': 'altCorrection1',
121-
# 'word': 'Street', 'corrections': ['St']}
122-
# ]
123-
# task = index.batch_synonyms(synonyms)
124-
# index.wait_task(task['taskID'])
125-
# it = SynonymIterator(index)
126-
# for got, expected in zip(it, synonyms):
127-
# self.assertEqual(got, expected)
116+
def test_iter_synonyms(index):
117+
synonyms = [{
118+
'objectID': 'city',
119+
'type': 'synonym',
120+
'synonyms': ['San Francisco', 'SF']
121+
}, {
122+
'objectID': 'street',
123+
'type': 'altCorrection1',
124+
'word': 'Street', 'corrections': ['St']
125+
}]
126+
127+
task = index.batch_synonyms(synonyms)
128+
index.wait_task(task['taskID'])
129+
130+
res = list(index.iter_synonyms(hits_per_page=1))
131+
assert len(res) == 2
132+
133+
for synonym in synonyms:
134+
assert synonym in res
128135

129136

130137
def test_facet_search(index):

tests/test_keys.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from helpers import wait_key, wait_missing_key
1+
from .helpers import wait_key, wait_missing_key
22

33

44
def test_list_user_keys(client):

0 commit comments

Comments
 (0)