Skip to content

Commit 8406eb2

Browse files
authored
fix(insights): retrieve index name from query if not returned by response (#1138)
1 parent f0f7a62 commit 8406eb2

File tree

5 files changed

+105
-54
lines changed

5 files changed

+105
-54
lines changed

packages/autocomplete-core/src/utils/__tests__/mapToAlgoliaResponse.test.ts

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -76,14 +76,10 @@ describe('mapToAlgoliaResponse', () => {
7676
expect.objectContaining({
7777
hits: [
7878
{
79-
__autocomplete_indexName: 'indexName',
80-
__autocomplete_queryID: 'queryID',
8179
label: 'Label 1',
8280
objectID: '1',
8381
},
8482
{
85-
__autocomplete_indexName: 'indexName',
86-
__autocomplete_queryID: 'queryID',
8783
label: 'Label 2',
8884
objectID: '2',
8985
},
@@ -92,14 +88,10 @@ describe('mapToAlgoliaResponse', () => {
9288
expect.objectContaining({
9389
hits: [
9490
{
95-
__autocomplete_indexName: 'indexName',
96-
__autocomplete_queryID: 'queryID',
9791
label: 'Label 3',
9892
objectID: '3',
9993
},
10094
{
101-
__autocomplete_indexName: 'indexName',
102-
__autocomplete_queryID: 'queryID',
10395
label: 'Label 4',
10496
objectID: '4',
10597
},
@@ -143,28 +135,20 @@ describe('mapToAlgoliaResponse', () => {
143135
expect(hits).toEqual([
144136
[
145137
{
146-
__autocomplete_indexName: 'indexName',
147-
__autocomplete_queryID: 'queryID',
148138
label: 'Label 1',
149139
objectID: '1',
150140
},
151141
{
152-
__autocomplete_indexName: 'indexName',
153-
__autocomplete_queryID: 'queryID',
154142
label: 'Label 2',
155143
objectID: '2',
156144
},
157145
],
158146
[
159147
{
160-
__autocomplete_indexName: 'indexName',
161-
__autocomplete_queryID: 'queryID',
162148
label: 'Label 3',
163149
objectID: '3',
164150
},
165151
{
166-
__autocomplete_indexName: 'indexName',
167-
__autocomplete_queryID: 'queryID',
168152
label: 'Label 4',
169153
objectID: '4',
170154
},

packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts

Lines changed: 3 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,12 @@ import type {
66
export function mapToAlgoliaResponse<THit>(
77
rawResults: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
88
) {
9-
const results: Array<SearchResponse<THit> | SearchForFacetValuesResponse> =
10-
rawResults.map((result) => {
11-
return {
12-
...result,
13-
hits: (result as SearchResponse<THit>).hits?.map((hit) => {
14-
// Bring support for the Insights plugin.
15-
return {
16-
...hit,
17-
__autocomplete_indexName: (result as SearchResponse<THit>).index,
18-
__autocomplete_queryID: (result as SearchResponse<THit>).queryID,
19-
};
20-
}),
21-
};
22-
});
23-
249
return {
25-
results,
26-
hits: results
10+
results: rawResults,
11+
hits: rawResults
2712
.map((result) => (result as SearchResponse<THit>).hits)
2813
.filter(Boolean),
29-
facetHits: results
14+
facetHits: rawResults
3015
.map((result) =>
3116
(result as SearchForFacetValuesResponse).facetHits?.map((facetHit) => {
3217
// Bring support for the highlighting components.

packages/autocomplete-js/src/__tests__/requester.test.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ describe('requester', () => {
265265
.map((node) => node.textContent)
266266
).toMatchInlineSnapshot(`
267267
Array [
268-
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}",
268+
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}",
269269
]
270270
`);
271271

@@ -304,7 +304,7 @@ describe('requester', () => {
304304
.map((node) => node.textContent)
305305
).toMatchInlineSnapshot(`
306306
Array [
307-
"{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":4}",
307+
"{\\"objectID\\":\\"7\\",\\"label\\":\\"Hit 7\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":4}",
308308
]
309309
`);
310310

@@ -316,8 +316,8 @@ describe('requester', () => {
316316
.map((node) => node.textContent)
317317
).toMatchInlineSnapshot(`
318318
Array [
319-
"{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":5}",
320-
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":6}",
319+
"{\\"objectID\\":\\"3\\",\\"label\\":\\"Hit 3\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":5}",
320+
"{\\"objectID\\":\\"4\\",\\"label\\":\\"Hit 4\\",\\"__autocomplete_indexName\\":\\"indexName2\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":6}",
321321
]
322322
`);
323323

@@ -331,7 +331,7 @@ describe('requester', () => {
331331
.map((node) => node.textContent)
332332
).toMatchInlineSnapshot(`
333333
Array [
334-
"{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":7}",
334+
"{\\"objectID\\":\\"5\\",\\"label\\":\\"Hit 5\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":7}",
335335
]
336336
`);
337337

@@ -561,7 +561,7 @@ describe('requester', () => {
561561
.map((node) => node.textContent)
562562
).toMatchInlineSnapshot(`
563563
Array [
564-
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}",
564+
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"hitsPerPage\\":20,\\"__autocomplete_id\\":0}",
565565
]
566566
`);
567567

@@ -675,7 +675,7 @@ describe('requester', () => {
675675
.map((node) => node.textContent)
676676
).toMatchInlineSnapshot(`
677677
Array [
678-
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"results\\":[{\\"page\\":0,\\"hitsPerPage\\":20,\\"nbHits\\":1,\\"nbPages\\":1,\\"processingTimeMS\\":0,\\"hits\\":[{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}}],\\"query\\":\\"\\",\\"params\\":\\"\\",\\"exhaustiveNbHits\\":true,\\"exhaustiveFacetsCount\\":true}],\\"facetHits\\":[],\\"__autocomplete_id\\":0}",
678+
"{\\"0\\":{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}},\\"results\\":[{\\"page\\":0,\\"hitsPerPage\\":20,\\"nbHits\\":1,\\"nbPages\\":1,\\"processingTimeMS\\":0,\\"hits\\":[{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"}}],\\"query\\":\\"\\",\\"params\\":\\"\\",\\"exhaustiveNbHits\\":true,\\"exhaustiveFacetsCount\\":true}],\\"facetHits\\":[],\\"__autocomplete_id\\":0}",
679679
]
680680
`);
681681

packages/autocomplete-preset-algolia/src/search/__tests__/fetchAlgoliaResults.test.ts

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,16 @@ function createTestSearchClient() {
1212
search: jest.fn(() =>
1313
Promise.resolve(
1414
createMultiSearchResponse<{ label: string }>(
15-
{ hits: [{ objectID: '1', label: 'Hit 1' }] },
16-
{ hits: [{ objectID: '2', label: 'Hit 2' }] }
15+
{
16+
index: 'indexName',
17+
hits: [{ objectID: '1', label: 'Hit 1' }],
18+
queryID: 'queryID1',
19+
},
20+
{
21+
index: 'indexName2',
22+
hits: [{ objectID: '2', label: 'Hit 2' }],
23+
queryID: 'queryID2',
24+
}
1725
)
1826
)
1927
),
@@ -49,6 +57,10 @@ describe('fetchAlgoliaResults', () => {
4957
indexName: 'indexName',
5058
query: 'query',
5159
},
60+
{
61+
indexName: 'indexName2',
62+
query: 'query',
63+
},
5264
],
5365
});
5466

@@ -63,13 +75,24 @@ describe('fetchAlgoliaResults', () => {
6375
highlightPostTag: '__/aa-highlight__',
6476
},
6577
},
78+
{
79+
indexName: 'indexName2',
80+
query: 'query',
81+
params: {
82+
hitsPerPage: 5,
83+
highlightPreTag: '__aa-highlight__',
84+
highlightPostTag: '__/aa-highlight__',
85+
},
86+
},
6687
]);
6788
expect(results).toEqual([
6889
expect.objectContaining({
6990
hits: [
7091
{
7192
objectID: '1',
7293
label: 'Hit 1',
94+
__autocomplete_indexName: 'indexName',
95+
__autocomplete_queryID: 'queryID1',
7396
__autocomplete_algoliaCredentials: {
7497
appId: 'algoliaAppId',
7598
apiKey: 'algoliaApiKey',
@@ -82,6 +105,8 @@ describe('fetchAlgoliaResults', () => {
82105
{
83106
objectID: '2',
84107
label: 'Hit 2',
108+
__autocomplete_indexName: 'indexName2',
109+
__autocomplete_queryID: 'queryID2',
85110
__autocomplete_algoliaCredentials: {
86111
appId: 'algoliaAppId',
87112
apiKey: 'algoliaApiKey',
@@ -108,6 +133,16 @@ describe('fetchAlgoliaResults', () => {
108133
page: 2,
109134
},
110135
},
136+
{
137+
indexName: 'indexName2',
138+
query: 'query',
139+
params: {
140+
hitsPerPage: 10,
141+
highlightPreTag: '<em>',
142+
highlightPostTag: '</em>',
143+
page: 2,
144+
},
145+
},
111146
],
112147
});
113148

@@ -123,13 +158,25 @@ describe('fetchAlgoliaResults', () => {
123158
page: 2,
124159
},
125160
},
161+
{
162+
indexName: 'indexName2',
163+
query: 'query',
164+
params: {
165+
hitsPerPage: 10,
166+
highlightPreTag: '<em>',
167+
highlightPostTag: '</em>',
168+
page: 2,
169+
},
170+
},
126171
]);
127172
expect(results).toEqual([
128173
expect.objectContaining({
129174
hits: [
130175
{
131176
objectID: '1',
132177
label: 'Hit 1',
178+
__autocomplete_indexName: 'indexName',
179+
__autocomplete_queryID: 'queryID1',
133180
__autocomplete_algoliaCredentials: {
134181
appId: 'algoliaAppId',
135182
apiKey: 'algoliaApiKey',
@@ -142,6 +189,8 @@ describe('fetchAlgoliaResults', () => {
142189
{
143190
objectID: '2',
144191
label: 'Hit 2',
192+
__autocomplete_indexName: 'indexName2',
193+
__autocomplete_queryID: 'queryID2',
145194
__autocomplete_algoliaCredentials: {
146195
appId: 'algoliaAppId',
147196
apiKey: 'algoliaApiKey',
@@ -152,25 +201,54 @@ describe('fetchAlgoliaResults', () => {
152201
]);
153202
});
154203

155-
test('attaches default Algolia agent', async () => {
156-
const searchClient = createTestSearchClient();
204+
test('retrieves index name from query when not returned by response', async () => {
205+
const searchClient = createSearchClient({
206+
search: jest.fn(() =>
207+
Promise.resolve(
208+
createMultiSearchResponse<{ label: string }>({
209+
hits: [{ objectID: '1', label: 'Hit 1' }],
210+
queryID: 'queryID1',
211+
})
212+
)
213+
),
214+
});
157215

158-
await fetchAlgoliaResults({
216+
const results = await fetchAlgoliaResults({
159217
searchClient,
160218
queries: [
161219
{
162220
indexName: 'indexName',
163221
query: 'query',
164-
params: {
165-
hitsPerPage: 10,
166-
highlightPreTag: '<em>',
167-
highlightPostTag: '</em>',
168-
page: 2,
169-
},
170222
},
171223
],
172224
});
173225

226+
expect(results).toEqual([
227+
expect.objectContaining({
228+
hits: [
229+
{
230+
objectID: '1',
231+
label: 'Hit 1',
232+
__autocomplete_indexName: 'indexName',
233+
__autocomplete_queryID: 'queryID1',
234+
__autocomplete_algoliaCredentials: {
235+
appId: 'algoliaAppId',
236+
apiKey: 'algoliaApiKey',
237+
},
238+
},
239+
],
240+
}),
241+
]);
242+
});
243+
244+
test('attaches default Algolia agent', async () => {
245+
const searchClient = createTestSearchClient();
246+
247+
await fetchAlgoliaResults({
248+
searchClient,
249+
queries: [{ indexName: 'indexName' }, { indexName: 'indexName2' }],
250+
});
251+
174252
expect(searchClient.addAlgoliaAgent).toHaveBeenCalledTimes(1);
175253
expect(searchClient.addAlgoliaAgent).toHaveBeenCalledWith(
176254
'autocomplete-core',
@@ -183,7 +261,7 @@ describe('fetchAlgoliaResults', () => {
183261

184262
await fetchAlgoliaResults({
185263
searchClient,
186-
queries: [],
264+
queries: [{ indexName: 'indexName1' }, { indexName: 'indexName2' }],
187265
userAgents: [{ segment: 'custom-ua', version: '1.0.0' }],
188266
});
189267

packages/autocomplete-preset-algolia/src/search/fetchAlgoliaResults.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,14 @@ export function fetchAlgoliaResults<TRecord>({
5555
})
5656
)
5757
.then((response) => {
58-
return response.results.map((result) => ({
58+
return response.results.map((result, resultIndex) => ({
5959
...result,
6060
hits: result.hits?.map((hit) => ({
6161
...hit,
62+
// Bring support for the Insights plugin.
63+
__autocomplete_indexName:
64+
result.index || queries[resultIndex].indexName,
65+
__autocomplete_queryID: result.queryID,
6266
__autocomplete_algoliaCredentials: {
6367
appId,
6468
apiKey,

0 commit comments

Comments
 (0)