Skip to content

Commit a9b682b

Browse files
Merge pull request #320 from algolia/feat/iter_rules
Feat/iter rules
2 parents 96c0ff0 + 6a25617 commit a9b682b

File tree

2 files changed

+45
-35
lines changed

2 files changed

+45
-35
lines changed

algoliasearch/index.py

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,38 +71,6 @@ def _load_next_page(self):
7171
self.cursor = self.answer.get('cursor', None)
7272

7373

74-
class RuleIterator:
75-
"""Iterator on the rules of an index"""
76-
77-
def __init__(self, index, hits_per_page=1000):
78-
self.index = index
79-
self.hits_per_page = hits_per_page
80-
self.page = 0
81-
82-
def __iter__(self):
83-
self._load_next_page()
84-
return self
85-
86-
def __next__(self):
87-
return self.next()
88-
89-
def next(self):
90-
if self.pos >= len(self.response['hits']):
91-
self._load_next_page()
92-
if self.pos < len(self.response['hits']):
93-
result = self.response['hits'][self.pos]
94-
self.pos += 1
95-
# Remove highlighting.
96-
if '_highlightResult' in result: del result['_highlightResult']
97-
return result
98-
else:
99-
raise StopIteration
100-
101-
def _load_next_page(self):
102-
self.response = self.index.search_rules('', page=self.page, hitsPerPage=self.hits_per_page)
103-
self.page += 1
104-
self.pos = 0
105-
10674
class Index(object):
10775
"""
10876
Contains all the functions related to one index.
@@ -683,9 +651,32 @@ def search_synonyms(self, query, types=[], page=0, hits_per_page=100, request_op
683651

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

686-
def iter_synonyms(self, hits_per_page=1000):
654+
def iter_synonyms(self, hits_per_page=1000, request_options=None):
655+
page = 0
656+
response = self.search_synonyms(
657+
'', page=page,
658+
hits_per_page=hits_per_page, request_options=request_options
659+
)
660+
661+
while response['hits']:
662+
for hit in response['hits']:
663+
if '_highlightResult' in hit:
664+
del hit['_highlightResult']
665+
666+
yield hit
667+
668+
page += 1
669+
response = self.search_synonyms(
670+
'', page=page,
671+
hits_per_page=hits_per_page, request_options=request_options
672+
)
673+
674+
def iter_rules(self, hits_per_page=1000, request_options=None):
687675
page = 0
688-
response = self.search_synonyms('', page=page, hits_per_page=hits_per_page)
676+
response = self.search_rules(
677+
'', page=page,
678+
hitsPerPage=hits_per_page, request_options=request_options
679+
)
689680

690681
while response['hits']:
691682
for hit in response['hits']:
@@ -695,7 +686,10 @@ def iter_synonyms(self, hits_per_page=1000):
695686
yield hit
696687

697688
page += 1
698-
response = self.search_synonyms('', page=page, hits_per_page=hits_per_page)
689+
response = self.search_rules(
690+
'', page=page,
691+
hitsPerPage=hits_per_page, request_options=request_options
692+
)
699693

700694
@deprecated
701695
def waitTask(self, task_id, time_before_retry=100):

tests/test_index.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,22 @@ def test_iter_synonyms(index):
134134
assert synonym in res
135135

136136

137+
def test_iter_rules(index):
138+
rules = [
139+
rule_stub('rule1'),
140+
rule_stub('rule2')
141+
]
142+
143+
task = index.batch_rules(rules)
144+
index.wait_task(task['taskID'])
145+
146+
res = list(index.iter_rules(hits_per_page=1))
147+
assert len(res) == 2
148+
149+
for rule in rules:
150+
assert rule in res
151+
152+
137153
def test_facet_search(index):
138154
settings = {'attributesForFacetting': ['searchable(series)', 'kind']}
139155
objects = [{

0 commit comments

Comments
 (0)