Skip to content

Commit

Permalink
fix(specs): correct searchSynonyms parameters (#2595)
Browse files Browse the repository at this point in the history
Co-authored-by: shortcuts <[email protected]>
  • Loading branch information
morganleroi and shortcuts authored Jan 23, 2024
1 parent 123234b commit 379fbc5
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 38 deletions.
2 changes: 1 addition & 1 deletion .github/.cache_version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
1.0.8
1.0.11
9 changes: 6 additions & 3 deletions specs/search/paths/synonyms/searchSynonyms.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ post:
description: Search for synonyms in your index. You can control and filter the search with parameters. To get all synonyms, send an empty request body.
parameters:
- $ref: '../../../common/parameters.yml#/IndexName'
- $ref: './common/parameters.yml#/Type'
- $ref: '../../../common/parameters.yml#/PageDefault0'
- $ref: '../../../common/parameters.yml#/HitsPerPage'
requestBody:
description: Body of the `searchSynonyms` operation.
content:
Expand All @@ -24,6 +21,12 @@ post:
properties:
query:
$ref: '../../../common/schemas/SearchParams.yml#/query'
type:
$ref: './common/parameters.yml#/SynonymType'
page:
$ref: '../../../common/schemas/SearchParams.yml#/page'
hitsPerPage:
$ref: '../../../common/schemas/IndexSettings.yml#/hitsPerPage'
responses:
'200':
description: OK
Expand Down
18 changes: 9 additions & 9 deletions templates/java/api_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -248,18 +248,19 @@ public <T> Iterable<T> browseObjects(String indexName, BrowseParamsObject params
* Helper: Returns an iterator on top of the `searchSynonyms` method.
*
* @param indexName The index in which to perform the request.
* @param type The synonym type. (optional)
* @param params The `searchSynonyms` parameters. (optional)
* @param requestOptions The requestOptions to send along with the query, they will be merged with the transporter requestOptions. (optional)
*/
public Iterable<SynonymHit> browseSynonyms(String indexName, SynonymType type, SearchSynonymsParams params, RequestOptions requestOptions) {
public Iterable<SynonymHit> browseSynonyms(String indexName, SearchSynonymsParams params, RequestOptions requestOptions) {
final Holder<Integer> currentPage = new Holder<>(0);
final int hitsPerPage = 1000;
params.setPage(0);
params.setHitsPerPage(1000);
return AlgoliaIterableHelper.createIterable(
() -> {
SearchSynonymsResponse response = this.searchSynonyms(indexName, type, currentPage.value, hitsPerPage, params, requestOptions);
currentPage.value = response.getNbHits() < hitsPerPage ? null : currentPage.value + 1;
SearchSynonymsResponse response = this.searchSynonyms(indexName, params, requestOptions);
currentPage.value = response.getNbHits() < params.getHitsPerPage() ? null : currentPage.value + 1;
return response.getHits().iterator();
},
() -> currentPage.value != null
Expand All @@ -270,11 +271,10 @@ public Iterable<SynonymHit> browseSynonyms(String indexName, SynonymType type, S
* Helper: Returns an iterator on top of the `searchSynonyms` method.
*
* @param indexName The index in which to perform the request.
* @param type The synonym type. (optional)
* @param params The `searchSynonyms` parameters .(optional)
*/
public Iterable<SynonymHit> browseSynonyms(String indexName, SynonymType type, SearchSynonymsParams params) {
return browseSynonyms(indexName, type, params, null);
public Iterable<SynonymHit> browseSynonyms(String indexName, SearchSynonymsParams params) {
return browseSynonyms(indexName, params, null);
}
Expand All @@ -284,7 +284,7 @@ public Iterable<SynonymHit> browseSynonyms(String indexName, SynonymType type, S
* @param indexName The index in which to perform the request.
*/
public Iterable<SynonymHit> browseSynonyms(String indexName) {
return browseSynonyms(indexName, null, null, null);
return browseSynonyms(indexName, null, null);
}
/**
Expand Down
19 changes: 11 additions & 8 deletions templates/javascript/clients/client/api/helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -196,31 +196,34 @@ browseRules(
* @param browseObjects.indexName - The index in which to perform the request.
* @param browseObjects.validate - The validator function. It receive the resolved return of the API call. By default, stops when there is less hits returned than the number of maximum hits (1000).
* @param browseObjects.aggregator - The function that runs right after the API call has been resolved, allows you to do anything with the response before `validate`.
* @param browseObjects.searchSynonymsParams - The `searchSynonyms` method parameters.
* @param requestOptions - The requestOptions to send along with the query, they will be forwarded to the `searchSynonyms` method and merged with the transporter requestOptions.
*/
browseSynonyms(
{
indexName,
validate,
aggregator,
...browseSynonymsOptions
searchSynonymsParams,
...browseSynonymsOptions,
}: BrowseOptions<SearchSynonymsResponse> & SearchSynonymsProps,
requestOptions?: RequestOptions
): Promise<SearchSynonymsResponse> {
const params = {
page: 0,
...searchSynonymsParams,
hitsPerPage: 1000,
...browseSynonymsOptions,
};

return createIterablePromise<SearchSynonymsResponse>({
func: (previousResponse) => {
return this.searchSynonyms(
{
...params,
indexName,
page: previousResponse
? previousResponse.page + 1
: browseSynonymsOptions.page || 0,
searchSynonymsParams: {
...params,
page: previousResponse
? previousResponse.page + 1
: params.page,
},
},
requestOptions
);
Expand Down
13 changes: 5 additions & 8 deletions templates/python/search_helpers.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -136,28 +136,25 @@
self,
index_name: str,
aggregator: Optional[Callable[[SearchSynonymsResponse], None]],
synonym_type: Optional[SynonymType] = None,
page: Optional[int] = 0,
hits_per_page: Optional[int] = 1000,
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
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(
index_name=index_name,
type=synonym_type,
page=page,
hits_per_page=hits_per_page,
search_synonyms_params=search_synonyms_params,
request_options=request_options,
)
return await create_iterable(
func=_func,
validate=lambda _resp: _resp.nb_hits < hits_per_page,
validate=lambda _resp: _resp.nb_hits < search_synonyms_params.hits_per_page,
aggregator=aggregator,
)
)
16 changes: 7 additions & 9 deletions tests/CTS/requests/search/searchSynonyms.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,21 @@
"testName": "searchSynonyms with all parameters",
"parameters": {
"indexName": "indexName",
"type": "altcorrection1",
"page": 10,
"hitsPerPage": 10,
"searchSynonymsParams": {
"query": "myQuery"
"query": "myQuery",
"type": "altcorrection1",
"page": 10,
"hitsPerPage": 10
}
},
"request": {
"path": "/1/indexes/indexName/synonyms/search",
"method": "POST",
"body": {
"query": "myQuery"
},
"queryParameters": {
"query": "myQuery",
"type": "altcorrection1",
"page": "10",
"hitsPerPage": "10"
"page": 10,
"hitsPerPage": 10
}
}
}
Expand Down

0 comments on commit 379fbc5

Please sign in to comment.