Skip to content

Commit 6254217

Browse files
authored
fix(clients): update browse iterator (#4058)
1 parent cc160af commit 6254217

File tree

10 files changed

+46
-129
lines changed

10 files changed

+46
-129
lines changed

clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ public async Task<IEnumerable<Rule>> BrowseRulesAsync(string indexName, SearchRu
344344
var page = prevResp?.Item2 ?? 0;
345345
var searchSynonymsResponse = await SearchRulesAsync(indexName, searchRulesParams, requestOptions);
346346
return new Tuple<SearchRulesResponse, int>(searchSynonymsResponse, page + 1);
347-
}, resp => resp?.Item1 is { NbHits: < hitsPerPage }).ConfigureAwait(false);
347+
}, resp => resp?.Item1 is { Hits.Count: < hitsPerPage }).ConfigureAwait(false);
348348

349349
return all.SelectMany(u => u.Item1.Hits);
350350
}
@@ -367,7 +367,7 @@ public async Task<IEnumerable<SynonymHit>> BrowseSynonymsAsync(string indexName,
367367
var searchSynonymsResponse = await SearchSynonymsAsync(indexName, synonymsParams, requestOptions);
368368
page = page + 1;
369369
return new Tuple<SearchSynonymsResponse, int>(searchSynonymsResponse, page);
370-
}, resp => resp?.Item1 is { NbHits: < hitsPerPage }).ConfigureAwait(false);
370+
}, resp => resp?.Item1 is { Hits.Count: < hitsPerPage }).ConfigureAwait(false);
371371

372372
return all.SelectMany(u => u.Item1.Hits);
373373
}

clients/algoliasearch-client-php/lib/Iterators/RuleIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function fetchNextPage()
1515
{
1616
if (
1717
is_array($this->response)
18-
&& $this->key >= $this->response['nbHits']
18+
&& $this->key >= count($this->response['hits'])
1919
) {
2020
return;
2121
}

clients/algoliasearch-client-php/lib/Iterators/SynonymIterator.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ protected function fetchNextPage()
1515
{
1616
if (
1717
is_array($this->response)
18-
&& $this->key >= $this->response['nbHits']
18+
&& $this->key >= count($this->response['hits'])
1919
) {
2020
return;
2121
}

clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,13 @@ public extension SearchClient {
243243
aggregator: @escaping (BrowseResponse<T>) -> Void,
244244
requestOptions: RequestOptions? = nil
245245
) async throws -> BrowseResponse<T> {
246-
try await createIterable(
246+
var updatedBrowseParams = browseParams
247+
if updatedBrowseParams.hitsPerPage == nil {
248+
updatedBrowseParams.hitsPerPage = 1000
249+
}
250+
251+
return try await createIterable(
247252
execute: { previousResponse in
248-
var updatedBrowseParams = browseParams
249253
if let previousResponse {
250254
updatedBrowseParams.cursor = previousResponse.cursor
251255
}
@@ -298,7 +302,7 @@ public extension SearchClient {
298302
)
299303
},
300304
validate: validate ?? { response in
301-
response.nbHits < hitsPerPage
305+
response.hits.count < hitsPerPage
302306
},
303307
aggregator: aggregator
304308
)
@@ -341,7 +345,7 @@ public extension SearchClient {
341345
)
342346
},
343347
validate: validate ?? { response in
344-
response.nbHits < hitsPerPage
348+
response.hits.count < hitsPerPage
345349
},
346350
aggregator: aggregator
347351
)

templates/go/search_helpers.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,10 @@ func (c *APIClient) BrowseObjects(
305305
browseParams BrowseParamsObject,
306306
opts ...IterableOption,
307307
) error {
308+
if browseParams.HitsPerPage == nil {
309+
browseParams.HitsPerPage = utils.ToPtr(int32(1000))
310+
}
311+
308312
_, err := CreateIterable( //nolint:wrapcheck
309313
func(previousResponse *BrowseResponse, previousErr error) (*BrowseResponse, error) {
310314
if previousResponse != nil {
@@ -363,7 +367,7 @@ func (c *APIClient) BrowseRules(
363367
)
364368
},
365369
func(response *SearchRulesResponse, err error) (bool, error) {
366-
return err != nil || (response != nil && response.NbHits < hitsPerPage), err
370+
return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err
367371
},
368372
opts...,
369373
)
@@ -409,7 +413,7 @@ func (c *APIClient) BrowseSynonyms(
409413
)
410414
},
411415
func(response *SearchSynonymsResponse, err error) (bool, error) {
412-
return err != nil || (response != nil && response.NbHits < hitsPerPage), err
416+
return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err
413417
},
414418
opts...,
415419
)

templates/java/api_helpers.mustache

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,10 @@ public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation) {
298298
public <T> Iterable<T> browseObjects(String indexName, BrowseParamsObject params, Class<T> innerType, RequestOptions requestOptions) {
299299
final Holder<String> currentCursor = new Holder<>();
300300
301+
if (params.getHitsPerPage() == null) {
302+
params.setHitsPerPage(1000);
303+
}
304+
301305
return AlgoliaIterableHelper.createIterable(
302306
() -> {
303307
BrowseResponse<T> response = this.browse(indexName, params, innerType, requestOptions);
@@ -347,7 +351,7 @@ public Iterable<SynonymHit> browseSynonyms(String indexName, SearchSynonymsParam
347351
return AlgoliaIterableHelper.createIterable(
348352
() -> {
349353
SearchSynonymsResponse response = this.searchSynonyms(indexName, params, requestOptions);
350-
currentPage.value = response.getNbHits() < params.getHitsPerPage() ? null : currentPage.value + 1;
354+
currentPage.value = response.getHits().size() < params.getHitsPerPage() ? null : currentPage.value + 1;
351355
return response.getHits().iterator();
352356
},
353357
() -> currentPage.value != null
@@ -389,7 +393,7 @@ public Iterable<Rule> browseRules(String indexName, SearchRulesParams params, Re
389393
return AlgoliaIterableHelper.createIterable(
390394
() -> {
391395
SearchRulesResponse response = this.searchRules(indexName, params.setPage(currentPage.value), requestOptions);
392-
currentPage.value = response.getNbHits() < hitsPerPage ? null : currentPage.value + 1;
396+
currentPage.value = response.getHits().size() < hitsPerPage ? null : currentPage.value + 1;
393397
return response.getHits().iterator();
394398
},
395399
() -> currentPage.value != null

templates/javascript/clients/client/api/helpers.mustache

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ browseObjects<T>(
176176
indexName,
177177
browseParams: {
178178
cursor: previousResponse ? previousResponse.cursor : undefined,
179+
hitsPerPage: 1000,
179180
...browseParams,
180181
},
181182
},
@@ -226,7 +227,7 @@ browseRules(
226227
requestOptions
227228
);
228229
},
229-
validate: (response) => response.nbHits < params.hitsPerPage,
230+
validate: (response) => response.hits.length < params.hitsPerPage,
230231
...browseRulesOptions,
231232
});
232233
},
@@ -271,7 +272,7 @@ browseSynonyms(
271272
params.page += 1;
272273
return resp;
273274
},
274-
validate: (response) => response.nbHits < params.hitsPerPage,
275+
validate: (response) => response.hits.length < params.hitsPerPage,
275276
...browseSynonymsOptions,
276277
});
277278
},

templates/python/search_helpers.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@
128128
"""
129129
Helper: Iterate on the `browse` method of the client to allow aggregating objects of an index.
130130
"""
131+
browse_params.hits_per_page = browse_params.hits_per_page or 1000
132+
131133
{{^isSyncClient}}async {{/isSyncClient}}def _func(_prev: Optional[BrowseResponse]) -> BrowseResponse:
132134
if _prev is not None and _prev.cursor is not None:
133135
browse_params.cursor = _prev.cursor
@@ -167,7 +169,7 @@
167169
)
168170
return {{^isSyncClient}}await {{/isSyncClient}}create_iterable{{#isSyncClient}}_sync{{/isSyncClient}}(
169171
func=_func,
170-
validate=lambda _resp: _resp.nb_hits < hits_per_page,
172+
validate=lambda _resp: len(_resp.hits) < hits_per_page,
171173
aggregator=aggregator,
172174
)
173175

@@ -197,7 +199,7 @@
197199
return resp
198200
return {{^isSyncClient}}await {{/isSyncClient}}create_iterable{{#isSyncClient}}_sync{{/isSyncClient}}(
199201
func=_func,
200-
validate=lambda _resp: _resp.nb_hits < hits_per_page,
202+
validate=lambda _resp: len(_resp.hits) < hits_per_page,
201203
aggregator=aggregator,
202204
)
203205

templates/ruby/search_helpers.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ end
9595
# @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `browse` method.
9696
# @param block [Proc] the block to execute on each object of the index.
9797
def browse_objects(index_name, browse_params = Search::BrowseParamsObject.new, request_options = {}, &block)
98+
browse_params[:hits_per_page] = browse_params[:hits_per_page] || 1000
99+
98100
hits = []
99101
loop do
100102
res = browse(index_name, browse_params, request_options)
@@ -130,7 +132,7 @@ def browse_rules(index_name, search_rules_params = Search::SearchRulesParams.new
130132
rules.concat(res.hits)
131133
end
132134
search_rules_params.page += 1
133-
break if res.nb_hits < search_rules_params.hits_per_page
135+
break if res.hits.length < search_rules_params.hits_per_page
134136
end
135137

136138
rules unless block_given?
@@ -154,7 +156,7 @@ def browse_synonyms(index_name, search_synonyms_params = Search::SearchSynonymsP
154156
synonyms.concat(res.hits)
155157
end
156158
search_synonyms_params.page += 1
157-
break if res.nb_hits < search_synonyms_params.hits_per_page
159+
break if res.hits.length < search_synonyms_params.hits_per_page
158160
end
159161

160162
synonyms unless block_given?

tests/output/csharp/src/ClientExtensionsTests.cs

Lines changed: 11 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -213,7 +213,7 @@ public async Task ShouldBrowseSynonyms()
213213
It.IsAny<CancellationToken>()
214214
)
215215
)
216-
// First call return 1000 Hits
216+
// Only one call since it will take the hit list length and conclude there isn't more
217217
.Returns(
218218
Task.FromResult(
219219
new AlgoliaHttpResponse
@@ -228,56 +228,8 @@ public async Task ShouldBrowseSynonyms()
228228
{
229229
new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 },
230230
new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 },
231-
}, // Not 1000 but it doesn't matter
232-
NbHits = 1000,
233-
}
234-
)
235-
)
236-
),
237-
}
238-
)
239-
)
240-
// Second call return again 1000 Hits
241-
.Returns(
242-
Task.FromResult(
243-
new AlgoliaHttpResponse
244-
{
245-
HttpStatusCode = 200,
246-
Body = new MemoryStream(
247-
Encoding.UTF8.GetBytes(
248-
serializer.Serialize(
249-
new SearchSynonymsResponse()
250-
{
251-
Hits = new List<SynonymHit>()
252-
{
253-
new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 },
254-
new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 },
255-
}, // Not 1000 but it doesn't matter
256-
NbHits = 1000,
257-
}
258-
)
259-
)
260-
),
261-
}
262-
)
263-
)
264-
// Third call return 999 Hits
265-
.Returns(
266-
Task.FromResult(
267-
new AlgoliaHttpResponse
268-
{
269-
HttpStatusCode = 200,
270-
Body = new MemoryStream(
271-
Encoding.UTF8.GetBytes(
272-
serializer.Serialize(
273-
new SearchSynonymsResponse
274-
{
275-
Hits = new List<SynonymHit>
276-
{
277-
new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 },
278-
new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 },
279-
}, // Not 1000 but it doesn't matter
280-
NbHits = 999,
231+
},
232+
NbHits = 2,
281233
}
282234
)
283235
)
@@ -302,10 +254,10 @@ public async Task ShouldBrowseSynonyms()
302254
It.IsAny<TimeSpan>(),
303255
It.IsAny<CancellationToken>()
304256
),
305-
Times.Exactly(3)
257+
Times.Exactly(1)
306258
);
307259

308-
Assert.Equal(6, browseSynonymsAsync.Count());
260+
Assert.Equal(2, browseSynonymsAsync.Count());
309261
}
310262

311263
[Fact]
@@ -323,59 +275,7 @@ public async Task ShouldBrowseRules()
323275
It.IsAny<CancellationToken>()
324276
)
325277
)
326-
// First call return 1000 Hits
327-
.Returns(
328-
Task.FromResult(
329-
new AlgoliaHttpResponse
330-
{
331-
HttpStatusCode = 200,
332-
Body = new MemoryStream(
333-
Encoding.UTF8.GetBytes(
334-
serializer.Serialize(
335-
new SearchRulesResponse
336-
{
337-
Page = 0,
338-
NbPages = 2,
339-
Hits = new List<Rule>
340-
{
341-
new() { ObjectID = "XXX" },
342-
new() { ObjectID = "XXX" },
343-
}, // Not 1000 but it doesn't matter
344-
NbHits = 1000,
345-
}
346-
)
347-
)
348-
),
349-
}
350-
)
351-
)
352-
// Second call return again 1000 Hits
353-
.Returns(
354-
Task.FromResult(
355-
new AlgoliaHttpResponse
356-
{
357-
HttpStatusCode = 200,
358-
Body = new MemoryStream(
359-
Encoding.UTF8.GetBytes(
360-
serializer.Serialize(
361-
new SearchRulesResponse
362-
{
363-
Page = 0,
364-
NbPages = 2,
365-
Hits = new List<Rule>
366-
{
367-
new() { ObjectID = "XXX" },
368-
new() { ObjectID = "XXX" },
369-
}, // Not 1000 but it doesn't matter
370-
NbHits = 1000,
371-
}
372-
)
373-
)
374-
),
375-
}
376-
)
377-
)
378-
// Third call return 999 Hits
278+
// Only one call since it will take the hit list length and conclude there isn't more
379279
.Returns(
380280
Task.FromResult(
381281
new AlgoliaHttpResponse
@@ -387,13 +287,13 @@ public async Task ShouldBrowseRules()
387287
new SearchRulesResponse
388288
{
389289
Page = 0,
390-
NbPages = 2,
290+
NbPages = 1,
391291
Hits = new List<Rule>
392292
{
393293
new() { ObjectID = "XXX" },
394294
new() { ObjectID = "XXX" },
395-
}, // Not 1000 but it doesn't matter
396-
NbHits = 999,
295+
},
296+
NbHits = 2,
397297
}
398298
)
399299
)
@@ -416,10 +316,10 @@ public async Task ShouldBrowseRules()
416316
It.IsAny<TimeSpan>(),
417317
It.IsAny<CancellationToken>()
418318
),
419-
Times.Exactly(3)
319+
Times.Exactly(1)
420320
);
421321

422-
Assert.Equal(6, browseSynonymsAsync.Count());
322+
Assert.Equal(2, browseSynonymsAsync.Count());
423323
}
424324

425325
[Fact]

0 commit comments

Comments
 (0)