Skip to content

Commit 2ac508f

Browse files
authored
fix(specs): search w/ hits & facets responses (#1774)
1 parent 769ef54 commit 2ac508f

File tree

14 files changed

+148
-83
lines changed

14 files changed

+148
-83
lines changed

clients/algoliasearch-client-dart/packages/algoliasearch/example/example.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ void main() async {
2929
queries: [querySuggestions, queryHits],
3030
);
3131
// Decompose the search results into separate results and suggestions.
32-
final [searchResult, suggestionsResult] = responseMulti.results;
32+
final [searchResult, suggestionsResult] = responseMulti.toList();
3333
// Print the search results and suggestions.
3434
printQuerySuggestion(searchResult);
3535
printHits(suggestionsResult);

clients/algoliasearch-client-dart/packages/algoliasearch/lib/src/extension.dart

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,16 +10,17 @@ extension SearchClientExt on SearchClient {
1010
searchMethodParams: SearchMethodParams(requests: [request]),
1111
requestOptions: requestOptions,
1212
);
13-
return response.results.first;
13+
return SearchResponse.fromJson(response.results.first);
1414
}
1515

1616
/// Perform a search operation targeting one index.
17-
Future<SearchResponses> searchMultiIndex({
17+
Future<Iterable<SearchResponse>> searchMultiIndex({
1818
required List<SearchForHits> queries,
1919
SearchStrategy? strategy,
2020
RequestOptions? requestOptions,
2121
}) {
2222
final request = SearchMethodParams(requests: queries, strategy: strategy);
23-
return search(searchMethodParams: request, requestOptions: requestOptions);
23+
return search(searchMethodParams: request, requestOptions: requestOptions)
24+
.then((res) => res.results.map((e) => SearchResponse.fromJson(e)));
2425
}
2526
}

clients/algoliasearch-client-dart/packages/client_search/example/example.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ void main() async {
1313
hitsPerPage: 5,
1414
);
1515
// Execute the search request.
16-
final responseHits = await client.searchIndex(
16+
final responseHits = await client.searchSingleIndex(
1717
indexName: 'instant_search',
18-
request: queryHits,
18+
searchParams: queryHits,
1919
);
2020
// Print the search hits.
2121
printHits(responseHits);

clients/algoliasearch-client-dart/packages/client_search/lib/src/extension.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,24 @@ import 'package:algolia_client_search/algolia_client_search.dart';
33
extension SearchClientExt on SearchClient {
44
/// Perform a search operation targeting one index.
55
Future<SearchResponse> searchIndex({
6-
required String indexName,
7-
required SearchParamsObject request,
6+
required SearchForHits request,
87
RequestOptions? requestOptions,
98
}) async {
10-
return searchSingleIndex(
11-
searchParams: request,
9+
final response = await search(
10+
searchMethodParams: SearchMethodParams(requests: [request]),
1211
requestOptions: requestOptions,
13-
indexName: indexName,
1412
);
13+
return SearchResponse.fromJson(response.results.first);
1514
}
1615

1716
/// Perform a search operation targeting one index.
18-
Future<SearchResponses> searchMultiIndex({
17+
Future<Iterable<SearchResponse>> searchMultiIndex({
1918
required List<SearchForHits> queries,
2019
SearchStrategy? strategy,
2120
RequestOptions? requestOptions,
22-
}) async {
21+
}) {
2322
final request = SearchMethodParams(requests: queries, strategy: strategy);
24-
return search(searchMethodParams: request, requestOptions: requestOptions);
23+
return search(searchMethodParams: request, requestOptions: requestOptions)
24+
.then((res) => res.results.map((e) => SearchResponse.fromJson(e)));
2525
}
2626
}

generators/src/main/java/com/algolia/codegen/AlgoliaJavaGenerator.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import io.swagger.v3.oas.models.media.Schema;
66
import io.swagger.v3.oas.models.servers.Server;
77
import java.util.*;
8+
import java.util.stream.Collectors;
89
import org.openapitools.codegen.*;
910
import org.openapitools.codegen.languages.JavaClientCodegen;
1011
import org.openapitools.codegen.model.ModelMap;
@@ -89,14 +90,26 @@ public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs)
8990
CodegenModel model = modelContainer.getModels().get(0).getModel();
9091

9192
if (!model.oneOf.isEmpty()) {
92-
List<HashMap<String, String>> oneOfList = new ArrayList();
93+
List<HashMap<String, Object>> oneOfList = new ArrayList();
9394

9495
for (String oneOf : model.oneOf) {
95-
HashMap<String, String> oneOfModel = new HashMap();
96-
96+
HashMap<String, Object> oneOfModel = new HashMap();
9797
oneOfModel.put("type", oneOf);
9898
oneOfModel.put("name", oneOf.replace("<", "Of").replace(">", ""));
99-
99+
oneOfModel.put("isList", oneOf.contains("List"));
100+
ModelsMap modelsMap = models.get(oneOf);
101+
if (modelsMap != null) {
102+
oneOfModel.put("isObject", true);
103+
CodegenModel compoundModel = modelsMap.getModels().get(0).getModel();
104+
List<String> values = (List<String>) compoundModel.vendorExtensions.get("x-discriminator-fields");
105+
if (values != null) {
106+
List<Map<String, String>> newValues = values
107+
.stream()
108+
.map(value -> Collections.singletonMap("field", value))
109+
.collect(Collectors.toList());
110+
oneOfModel.put("discriminators", newValues);
111+
}
112+
}
100113
oneOfList.add(oneOfModel);
101114
}
102115

generators/src/main/java/com/algolia/codegen/AlgoliaKotlinGenerator.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.*;
88
import java.util.regex.Matcher;
99
import java.util.regex.Pattern;
10+
import java.util.stream.Collectors;
1011
import org.apache.commons.lang3.StringUtils;
1112
import org.openapitools.codegen.*;
1213
import org.openapitools.codegen.languages.KotlinClientCodegen;
@@ -218,6 +219,14 @@ private void modelsOneOf(Map<String, ModelsMap> models) {
218219
HashMap<String, String> parentInfo = new HashMap<>();
219220
parentInfo.put("parent_classname", model.classname);
220221
compoundParent(compoundModel).add(parentInfo);
222+
List<String> values = (List<String>) compoundModel.vendorExtensions.get("x-discriminator-fields");
223+
if (values != null) {
224+
List<Map<String, String>> newValues = values
225+
.stream()
226+
.map(value -> Collections.singletonMap("field", value))
227+
.collect(Collectors.toList());
228+
oneOfModel.put("discriminators", newValues);
229+
}
221230
}
222231
oneOfList.add(oneOfModel);
223232
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
searchForFacetValuesResponse:
2+
type: object
3+
additionalProperties: false
4+
required:
5+
- facetHits
6+
x-discriminator-fields:
7+
- facetHits
8+
properties:
9+
facetHits:
10+
type: array
11+
items:
12+
type: object
13+
title: facetHits
14+
additionalProperties: false
15+
required:
16+
- value
17+
- highlighted
18+
- count
19+
properties:
20+
value:
21+
description: Facet value.
22+
example: 'Mobile phone'
23+
type: string
24+
highlighted:
25+
$ref: '../../common/schemas/Hit.yml#/highlightedValue'
26+
count:
27+
description: Number of records containing this facet value. This takes into account the extra search parameters specified in the query. Like for a regular search query, the [counts may not be exhaustive](https://support.algolia.com/hc/en-us/articles/4406975248145-Why-are-my-facet-and-hit-counts-not-accurate-).
28+
type: integer
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
searchResult:
2+
oneOf:
3+
- $ref: 'SearchResponse.yml#/searchResponse'
4+
- $ref: 'SearchForFacetValuesResponse.yml#/searchForFacetValuesResponse'

specs/search/paths/search/search.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ post:
3838
results:
3939
type: array
4040
items:
41-
$ref: '../../common/schemas/SearchResponse.yml#/searchResponse'
41+
$ref: '../../common/schemas/SearchResult.yml#/searchResult'
4242
required:
4343
- results
4444
'400':

specs/search/paths/search/searchForFacetValues.yml

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -37,32 +37,7 @@ post:
3737
content:
3838
application/json:
3939
schema:
40-
title: searchForFacetValuesResponse
41-
type: object
42-
additionalProperties: false
43-
required:
44-
- facetHits
45-
properties:
46-
facetHits:
47-
type: array
48-
items:
49-
type: object
50-
title: facetHits
51-
additionalProperties: false
52-
required:
53-
- value
54-
- highlighted
55-
- count
56-
properties:
57-
value:
58-
description: Facet value.
59-
example: 'Mobile phone'
60-
type: string
61-
highlighted:
62-
$ref: '../../common/schemas/Hit.yml#/highlightedValue'
63-
count:
64-
description: Number of records containing this facet value. This takes into account the extra search parameters specified in the query. Like for a regular search query, the [counts may not be exhaustive](https://support.algolia.com/hc/en-us/articles/4406975248145-Why-are-my-facet-and-hit-counts-not-accurate-).
65-
type: integer
40+
$ref: '../../common/schemas/SearchForFacetValuesResponse.yml#/searchForFacetValuesResponse'
6641
'400':
6742
$ref: '../../../common/responses/BadRequest.yml'
6843
'402':

0 commit comments

Comments
 (0)