diff --git a/clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs b/clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs index d621aa0972..04854c3db9 100644 --- a/clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs +++ b/clients/algoliasearch-client-csharp/algoliasearch/Utils/SearchClientExtensions.cs @@ -344,7 +344,7 @@ public async Task> BrowseRulesAsync(string indexName, SearchRu var page = prevResp?.Item2 ?? 0; var searchSynonymsResponse = await SearchRulesAsync(indexName, searchRulesParams, requestOptions); return new Tuple(searchSynonymsResponse, page + 1); - }, resp => resp?.Item1 is { NbHits: < hitsPerPage }).ConfigureAwait(false); + }, resp => resp?.Item1 is { Hits.Count: < hitsPerPage }).ConfigureAwait(false); return all.SelectMany(u => u.Item1.Hits); } @@ -367,7 +367,7 @@ public async Task> BrowseSynonymsAsync(string indexName, var searchSynonymsResponse = await SearchSynonymsAsync(indexName, synonymsParams, requestOptions); page = page + 1; return new Tuple(searchSynonymsResponse, page); - }, resp => resp?.Item1 is { NbHits: < hitsPerPage }).ConfigureAwait(false); + }, resp => resp?.Item1 is { Hits.Count: < hitsPerPage }).ConfigureAwait(false); return all.SelectMany(u => u.Item1.Hits); } diff --git a/clients/algoliasearch-client-php/lib/Iterators/RuleIterator.php b/clients/algoliasearch-client-php/lib/Iterators/RuleIterator.php index 5dc3de34f5..2b1259c494 100644 --- a/clients/algoliasearch-client-php/lib/Iterators/RuleIterator.php +++ b/clients/algoliasearch-client-php/lib/Iterators/RuleIterator.php @@ -15,7 +15,7 @@ protected function fetchNextPage() { if ( is_array($this->response) - && $this->key >= $this->response['nbHits'] + && $this->key >= count($this->response['hits']) ) { return; } diff --git a/clients/algoliasearch-client-php/lib/Iterators/SynonymIterator.php b/clients/algoliasearch-client-php/lib/Iterators/SynonymIterator.php index 7c2d3f0c15..6cc7f4ceae 100644 --- a/clients/algoliasearch-client-php/lib/Iterators/SynonymIterator.php +++ b/clients/algoliasearch-client-php/lib/Iterators/SynonymIterator.php @@ -15,7 +15,7 @@ protected function fetchNextPage() { if ( is_array($this->response) - && $this->key >= $this->response['nbHits'] + && $this->key >= count($this->response['hits']) ) { return; } diff --git a/clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift b/clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift index fefa966a70..8870757f8c 100644 --- a/clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift +++ b/clients/algoliasearch-client-swift/Sources/Search/Extra/SearchClientExtension.swift @@ -243,9 +243,13 @@ public extension SearchClient { aggregator: @escaping (BrowseResponse) -> Void, requestOptions: RequestOptions? = nil ) async throws -> BrowseResponse { - try await createIterable( + var updatedBrowseParams = browseParams + if updatedBrowseParams.hitsPerPage == nil { + updatedBrowseParams.hitsPerPage = 1000 + } + + return try await createIterable( execute: { previousResponse in - var updatedBrowseParams = browseParams if let previousResponse { updatedBrowseParams.cursor = previousResponse.cursor } @@ -298,7 +302,7 @@ public extension SearchClient { ) }, validate: validate ?? { response in - response.nbHits < hitsPerPage + response.hits.count < hitsPerPage }, aggregator: aggregator ) @@ -341,7 +345,7 @@ public extension SearchClient { ) }, validate: validate ?? { response in - response.nbHits < hitsPerPage + response.hits.count < hitsPerPage }, aggregator: aggregator ) diff --git a/templates/go/search_helpers.mustache b/templates/go/search_helpers.mustache index 0845019265..c40b20fb85 100644 --- a/templates/go/search_helpers.mustache +++ b/templates/go/search_helpers.mustache @@ -305,6 +305,10 @@ func (c *APIClient) BrowseObjects( browseParams BrowseParamsObject, opts ...IterableOption, ) error { + if browseParams.HitsPerPage == nil { + browseParams.HitsPerPage = utils.ToPtr(int32(1000)) + } + _, err := CreateIterable( //nolint:wrapcheck func(previousResponse *BrowseResponse, previousErr error) (*BrowseResponse, error) { if previousResponse != nil { @@ -363,7 +367,7 @@ func (c *APIClient) BrowseRules( ) }, func(response *SearchRulesResponse, err error) (bool, error) { - return err != nil || (response != nil && response.NbHits < hitsPerPage), err + return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err }, opts..., ) @@ -409,7 +413,7 @@ func (c *APIClient) BrowseSynonyms( ) }, func(response *SearchSynonymsResponse, err error) (bool, error) { - return err != nil || (response != nil && response.NbHits < hitsPerPage), err + return err != nil || (response != nil && len(response.Hits) < int(hitsPerPage)), err }, opts..., ) diff --git a/templates/java/api_helpers.mustache b/templates/java/api_helpers.mustache index 80213a82fb..439b192422 100644 --- a/templates/java/api_helpers.mustache +++ b/templates/java/api_helpers.mustache @@ -298,6 +298,10 @@ public GetApiKeyResponse waitForApiKey(String key, ApiKeyOperation operation) { public Iterable browseObjects(String indexName, BrowseParamsObject params, Class innerType, RequestOptions requestOptions) { final Holder currentCursor = new Holder<>(); + if (params.getHitsPerPage() == null) { + params.setHitsPerPage(1000); + } + return AlgoliaIterableHelper.createIterable( () -> { BrowseResponse response = this.browse(indexName, params, innerType, requestOptions); @@ -347,7 +351,7 @@ public Iterable browseSynonyms(String indexName, SearchSynonymsParam return AlgoliaIterableHelper.createIterable( () -> { SearchSynonymsResponse response = this.searchSynonyms(indexName, params, requestOptions); - currentPage.value = response.getNbHits() < params.getHitsPerPage() ? null : currentPage.value + 1; + currentPage.value = response.getHits().size() < params.getHitsPerPage() ? null : currentPage.value + 1; return response.getHits().iterator(); }, () -> currentPage.value != null @@ -389,7 +393,7 @@ public Iterable browseRules(String indexName, SearchRulesParams params, Re return AlgoliaIterableHelper.createIterable( () -> { SearchRulesResponse response = this.searchRules(indexName, params.setPage(currentPage.value), requestOptions); - currentPage.value = response.getNbHits() < hitsPerPage ? null : currentPage.value + 1; + currentPage.value = response.getHits().size() < hitsPerPage ? null : currentPage.value + 1; return response.getHits().iterator(); }, () -> currentPage.value != null diff --git a/templates/javascript/clients/client/api/helpers.mustache b/templates/javascript/clients/client/api/helpers.mustache index 7610e01fef..3feea7cf5e 100644 --- a/templates/javascript/clients/client/api/helpers.mustache +++ b/templates/javascript/clients/client/api/helpers.mustache @@ -176,6 +176,7 @@ browseObjects( indexName, browseParams: { cursor: previousResponse ? previousResponse.cursor : undefined, + hitsPerPage: 1000, ...browseParams, }, }, @@ -226,7 +227,7 @@ browseRules( requestOptions ); }, - validate: (response) => response.nbHits < params.hitsPerPage, + validate: (response) => response.hits.length < params.hitsPerPage, ...browseRulesOptions, }); }, @@ -271,7 +272,7 @@ browseSynonyms( params.page += 1; return resp; }, - validate: (response) => response.nbHits < params.hitsPerPage, + validate: (response) => response.hits.length < params.hitsPerPage, ...browseSynonymsOptions, }); }, diff --git a/templates/python/search_helpers.mustache b/templates/python/search_helpers.mustache index 80a7343761..f7d214eefe 100644 --- a/templates/python/search_helpers.mustache +++ b/templates/python/search_helpers.mustache @@ -128,6 +128,8 @@ """ Helper: Iterate on the `browse` method of the client to allow aggregating objects of an index. """ + browse_params.hits_per_page = browse_params.hits_per_page or 1000 + {{^isSyncClient}}async {{/isSyncClient}}def _func(_prev: Optional[BrowseResponse]) -> BrowseResponse: if _prev is not None and _prev.cursor is not None: browse_params.cursor = _prev.cursor @@ -167,7 +169,7 @@ ) return {{^isSyncClient}}await {{/isSyncClient}}create_iterable{{#isSyncClient}}_sync{{/isSyncClient}}( func=_func, - validate=lambda _resp: _resp.nb_hits < hits_per_page, + validate=lambda _resp: len(_resp.hits) < hits_per_page, aggregator=aggregator, ) @@ -197,7 +199,7 @@ return resp return {{^isSyncClient}}await {{/isSyncClient}}create_iterable{{#isSyncClient}}_sync{{/isSyncClient}}( func=_func, - validate=lambda _resp: _resp.nb_hits < hits_per_page, + validate=lambda _resp: len(_resp.hits) < hits_per_page, aggregator=aggregator, ) diff --git a/templates/ruby/search_helpers.mustache b/templates/ruby/search_helpers.mustache index 2c54d095a3..d602cf3855 100644 --- a/templates/ruby/search_helpers.mustache +++ b/templates/ruby/search_helpers.mustache @@ -95,6 +95,8 @@ end # @param request_options [Hash] the requestOptions to send along with the query, they will be forwarded to the `browse` method. # @param block [Proc] the block to execute on each object of the index. def browse_objects(index_name, browse_params = Search::BrowseParamsObject.new, request_options = {}, &block) + browse_params[:hits_per_page] = browse_params[:hits_per_page] || 1000 + hits = [] loop do res = browse(index_name, browse_params, request_options) @@ -130,7 +132,7 @@ def browse_rules(index_name, search_rules_params = Search::SearchRulesParams.new rules.concat(res.hits) end search_rules_params.page += 1 - break if res.nb_hits < search_rules_params.hits_per_page + break if res.hits.length < search_rules_params.hits_per_page end rules unless block_given? @@ -154,7 +156,7 @@ def browse_synonyms(index_name, search_synonyms_params = Search::SearchSynonymsP synonyms.concat(res.hits) end search_synonyms_params.page += 1 - break if res.nb_hits < search_synonyms_params.hits_per_page + break if res.hits.length < search_synonyms_params.hits_per_page end synonyms unless block_given? diff --git a/tests/output/csharp/src/ClientExtensionsTests.cs b/tests/output/csharp/src/ClientExtensionsTests.cs index b399fbb096..dd304dbf54 100644 --- a/tests/output/csharp/src/ClientExtensionsTests.cs +++ b/tests/output/csharp/src/ClientExtensionsTests.cs @@ -213,7 +213,7 @@ public async Task ShouldBrowseSynonyms() It.IsAny() ) ) - // First call return 1000 Hits + // Only one call since it will take the hit list length and conclude there isn't more .Returns( Task.FromResult( new AlgoliaHttpResponse @@ -228,56 +228,8 @@ public async Task ShouldBrowseSynonyms() { new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 }, new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 }, - }, // Not 1000 but it doesn't matter - NbHits = 1000, - } - ) - ) - ), - } - ) - ) - // Second call return again 1000 Hits - .Returns( - Task.FromResult( - new AlgoliaHttpResponse - { - HttpStatusCode = 200, - Body = new MemoryStream( - Encoding.UTF8.GetBytes( - serializer.Serialize( - new SearchSynonymsResponse() - { - Hits = new List() - { - new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 }, - new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 }, - }, // Not 1000 but it doesn't matter - NbHits = 1000, - } - ) - ) - ), - } - ) - ) - // Third call return 999 Hits - .Returns( - Task.FromResult( - new AlgoliaHttpResponse - { - HttpStatusCode = 200, - Body = new MemoryStream( - Encoding.UTF8.GetBytes( - serializer.Serialize( - new SearchSynonymsResponse - { - Hits = new List - { - new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 }, - new() { ObjectID = "XXX", Type = SynonymType.Altcorrection1 }, - }, // Not 1000 but it doesn't matter - NbHits = 999, + }, + NbHits = 2, } ) ) @@ -302,10 +254,10 @@ public async Task ShouldBrowseSynonyms() It.IsAny(), It.IsAny() ), - Times.Exactly(3) + Times.Exactly(1) ); - Assert.Equal(6, browseSynonymsAsync.Count()); + Assert.Equal(2, browseSynonymsAsync.Count()); } [Fact] @@ -323,59 +275,7 @@ public async Task ShouldBrowseRules() It.IsAny() ) ) - // First call return 1000 Hits - .Returns( - Task.FromResult( - new AlgoliaHttpResponse - { - HttpStatusCode = 200, - Body = new MemoryStream( - Encoding.UTF8.GetBytes( - serializer.Serialize( - new SearchRulesResponse - { - Page = 0, - NbPages = 2, - Hits = new List - { - new() { ObjectID = "XXX" }, - new() { ObjectID = "XXX" }, - }, // Not 1000 but it doesn't matter - NbHits = 1000, - } - ) - ) - ), - } - ) - ) - // Second call return again 1000 Hits - .Returns( - Task.FromResult( - new AlgoliaHttpResponse - { - HttpStatusCode = 200, - Body = new MemoryStream( - Encoding.UTF8.GetBytes( - serializer.Serialize( - new SearchRulesResponse - { - Page = 0, - NbPages = 2, - Hits = new List - { - new() { ObjectID = "XXX" }, - new() { ObjectID = "XXX" }, - }, // Not 1000 but it doesn't matter - NbHits = 1000, - } - ) - ) - ), - } - ) - ) - // Third call return 999 Hits + // Only one call since it will take the hit list length and conclude there isn't more .Returns( Task.FromResult( new AlgoliaHttpResponse @@ -387,13 +287,13 @@ public async Task ShouldBrowseRules() new SearchRulesResponse { Page = 0, - NbPages = 2, + NbPages = 1, Hits = new List { new() { ObjectID = "XXX" }, new() { ObjectID = "XXX" }, - }, // Not 1000 but it doesn't matter - NbHits = 999, + }, + NbHits = 2, } ) ) @@ -416,10 +316,10 @@ public async Task ShouldBrowseRules() It.IsAny(), It.IsAny() ), - Times.Exactly(3) + Times.Exactly(1) ); - Assert.Equal(6, browseSynonymsAsync.Count()); + Assert.Equal(2, browseSynonymsAsync.Count()); } [Fact]