Skip to content

Commit

Permalink
fix(clients): browse synonyms page not in response (#2757)
Browse files Browse the repository at this point in the history
  • Loading branch information
shortcuts authored Feb 21, 2024
1 parent d5600d4 commit 92cd1c7
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,12 +236,13 @@ public static async Task<IEnumerable<SynonymHit>> BrowseSynonymsAsync(this Searc
RequestOptions requestOptions = null)
{
const int hitsPerPage = 1000;
var page = synonymsParams.Page ?? 0;
synonymsParams.HitsPerPage = hitsPerPage;
var all = await CreateIterable<Tuple<SearchSynonymsResponse, int>>(async (prevResp) =>
{
synonymsParams.Page = prevResp?.Item2 ?? 0;
var searchSynonymsResponse = await client.SearchSynonymsAsync(indexName, synonymsParams, requestOptions);
return new Tuple<SearchSynonymsResponse, int>(searchSynonymsResponse, (prevResp?.Item2 ?? 0) + 1);
page = page + 1;
return new Tuple<SearchSynonymsResponse, int>(searchSynonymsResponse, page);
}, resp => resp?.Item1 is { NbHits: < hitsPerPage }).ConfigureAwait(false);

return all.SelectMany(u => u.Item1.Hits);
Expand Down
16 changes: 3 additions & 13 deletions playground/python/app/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
from os import environ

from algoliasearch.search.client import SearchClient
from algoliasearch.search.models.search_method_params import SearchMethodParams
from algoliasearch.search.models.search_for_hits import SearchForHits
from algoliasearch.search.models.search_query import SearchQuery
from algoliasearch.search import __version__
from dotenv import load_dotenv

Expand All @@ -18,16 +15,9 @@ async def main():
print("client initialized", client)

try:
response = await client.search(
search_method_params=SearchMethodParams(
requests=[
SearchQuery(SearchForHits(index_name="cts_e2e_search_facet")),
],
),
)

print("client response")
print(response.to_json())
resp = await client.browse_synonyms(index_name="test-flag", aggregator=lambda _resp: print(_resp))

print(resp)
finally:
await client.close()

Expand Down
10 changes: 5 additions & 5 deletions templates/javascript/clients/client/api/helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -214,19 +214,19 @@ browseSynonyms(
};

return createIterablePromise<SearchSynonymsResponse>({
func: (previousResponse) => {
return this.searchSynonyms(
func: (_) => {
const resp = this.searchSynonyms(
{
indexName,
searchSynonymsParams: {
...params,
page: previousResponse
? previousResponse.page + 1
: params.page,
page: params.page,
},
},
requestOptions
);
params.page += 1;
return resp;
},
validate: (response) => response.nbHits < params.hitsPerPage,
...browseSynonymsOptions,
Expand Down
11 changes: 6 additions & 5 deletions templates/python/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -135,24 +135,25 @@
async def browse_synonyms(
self,
index_name: str,
aggregator: Optional[Callable[[SearchSynonymsResponse], None]],
aggregator: Callable[[SearchSynonymsResponse], None],
search_synonyms_params: Optional[SearchSynonymsParams] = SearchSynonymsParams(),
request_options: Optional[Union[dict, RequestOptions]] = None,
) -> SearchSynonymsResponse:
"""
Helper: Iterate on the `search_synonyms` method of the client to allow aggregating synonyms of an index.
"""
search_synonyms_params.page = 0
if search_synonyms_params.page is None:
search_synonyms_params.page = 0
search_synonyms_params.hits_per_page = 1000

async def _func(_prev: SearchRulesResponse) -> SearchRulesResponse:
if _prev is not None:
search_synonyms_params.page = _prev.page + 1
return await self.search_synonyms(
resp = await self.search_synonyms(
index_name=index_name,
search_synonyms_params=search_synonyms_params,
request_options=request_options,
)
search_synonyms_params.page += 1
return resp
return await create_iterable(
func=_func,
validate=lambda _resp: _resp.nb_hits < search_synonyms_params.hits_per_page,
Expand Down

0 comments on commit 92cd1c7

Please sign in to comment.