Skip to content

Commit

Permalink
fix(insights): retrieve index name from query if not returned by resp…
Browse files Browse the repository at this point in the history
…onse (#1138)
  • Loading branch information
dhayab committed May 2, 2023
1 parent f0f7a62 commit 8406eb2
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 54 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,10 @@ describe('mapToAlgoliaResponse', () => {
expect.objectContaining({
hits: [
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 1',
objectID: '1',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 2',
objectID: '2',
},
Expand All @@ -92,14 +88,10 @@ describe('mapToAlgoliaResponse', () => {
expect.objectContaining({
hits: [
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 3',
objectID: '3',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 4',
objectID: '4',
},
Expand Down Expand Up @@ -143,28 +135,20 @@ describe('mapToAlgoliaResponse', () => {
expect(hits).toEqual([
[
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 1',
objectID: '1',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 2',
objectID: '2',
},
],
[
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 3',
objectID: '3',
},
{
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID',
label: 'Label 4',
objectID: '4',
},
Expand Down
21 changes: 3 additions & 18 deletions packages/autocomplete-core/src/utils/mapToAlgoliaResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,12 @@ import type {
export function mapToAlgoliaResponse<THit>(
rawResults: Array<SearchResponse<THit> | SearchForFacetValuesResponse>
) {
const results: Array<SearchResponse<THit> | SearchForFacetValuesResponse> =
rawResults.map((result) => {
return {
...result,
hits: (result as SearchResponse<THit>).hits?.map((hit) => {
// Bring support for the Insights plugin.
return {
...hit,
__autocomplete_indexName: (result as SearchResponse<THit>).index,
__autocomplete_queryID: (result as SearchResponse<THit>).queryID,
};
}),
};
});

return {
results,
hits: results
results: rawResults,
hits: rawResults
.map((result) => (result as SearchResponse<THit>).hits)
.filter(Boolean),
facetHits: results
facetHits: rawResults
.map((result) =>
(result as SearchForFacetValuesResponse).facetHits?.map((facetHit) => {
// Bring support for the highlighting components.
Expand Down
14 changes: 7 additions & 7 deletions packages/autocomplete-js/src/__tests__/requester.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}",
"{\\"objectID\\":\\"1\\",\\"label\\":\\"Hit 1\\",\\"__autocomplete_indexName\\":\\"indexName\\",\\"__autocomplete_algoliaCredentials\\":{\\"appId\\":\\"algoliaAppId\\",\\"apiKey\\":\\"algoliaApiKey\\"},\\"__autocomplete_id\\":0}",
]
`);

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

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

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

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

Expand Down Expand Up @@ -675,7 +675,7 @@ describe('requester', () => {
.map((node) => node.textContent)
).toMatchInlineSnapshot(`
Array [
"{\\"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}",
"{\\"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}",
]
`);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,16 @@ function createTestSearchClient() {
search: jest.fn(() =>
Promise.resolve(
createMultiSearchResponse<{ label: string }>(
{ hits: [{ objectID: '1', label: 'Hit 1' }] },
{ hits: [{ objectID: '2', label: 'Hit 2' }] }
{
index: 'indexName',
hits: [{ objectID: '1', label: 'Hit 1' }],
queryID: 'queryID1',
},
{
index: 'indexName2',
hits: [{ objectID: '2', label: 'Hit 2' }],
queryID: 'queryID2',
}
)
)
),
Expand Down Expand Up @@ -49,6 +57,10 @@ describe('fetchAlgoliaResults', () => {
indexName: 'indexName',
query: 'query',
},
{
indexName: 'indexName2',
query: 'query',
},
],
});

Expand All @@ -63,13 +75,24 @@ describe('fetchAlgoliaResults', () => {
highlightPostTag: '__/aa-highlight__',
},
},
{
indexName: 'indexName2',
query: 'query',
params: {
hitsPerPage: 5,
highlightPreTag: '__aa-highlight__',
highlightPostTag: '__/aa-highlight__',
},
},
]);
expect(results).toEqual([
expect.objectContaining({
hits: [
{
objectID: '1',
label: 'Hit 1',
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID1',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
Expand All @@ -82,6 +105,8 @@ describe('fetchAlgoliaResults', () => {
{
objectID: '2',
label: 'Hit 2',
__autocomplete_indexName: 'indexName2',
__autocomplete_queryID: 'queryID2',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
Expand All @@ -108,6 +133,16 @@ describe('fetchAlgoliaResults', () => {
page: 2,
},
},
{
indexName: 'indexName2',
query: 'query',
params: {
hitsPerPage: 10,
highlightPreTag: '<em>',
highlightPostTag: '</em>',
page: 2,
},
},
],
});

Expand All @@ -123,13 +158,25 @@ describe('fetchAlgoliaResults', () => {
page: 2,
},
},
{
indexName: 'indexName2',
query: 'query',
params: {
hitsPerPage: 10,
highlightPreTag: '<em>',
highlightPostTag: '</em>',
page: 2,
},
},
]);
expect(results).toEqual([
expect.objectContaining({
hits: [
{
objectID: '1',
label: 'Hit 1',
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID1',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
Expand All @@ -142,6 +189,8 @@ describe('fetchAlgoliaResults', () => {
{
objectID: '2',
label: 'Hit 2',
__autocomplete_indexName: 'indexName2',
__autocomplete_queryID: 'queryID2',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
Expand All @@ -152,25 +201,54 @@ describe('fetchAlgoliaResults', () => {
]);
});

test('attaches default Algolia agent', async () => {
const searchClient = createTestSearchClient();
test('retrieves index name from query when not returned by response', async () => {
const searchClient = createSearchClient({
search: jest.fn(() =>
Promise.resolve(
createMultiSearchResponse<{ label: string }>({
hits: [{ objectID: '1', label: 'Hit 1' }],
queryID: 'queryID1',
})
)
),
});

await fetchAlgoliaResults({
const results = await fetchAlgoliaResults({
searchClient,
queries: [
{
indexName: 'indexName',
query: 'query',
params: {
hitsPerPage: 10,
highlightPreTag: '<em>',
highlightPostTag: '</em>',
page: 2,
},
},
],
});

expect(results).toEqual([
expect.objectContaining({
hits: [
{
objectID: '1',
label: 'Hit 1',
__autocomplete_indexName: 'indexName',
__autocomplete_queryID: 'queryID1',
__autocomplete_algoliaCredentials: {
appId: 'algoliaAppId',
apiKey: 'algoliaApiKey',
},
},
],
}),
]);
});

test('attaches default Algolia agent', async () => {
const searchClient = createTestSearchClient();

await fetchAlgoliaResults({
searchClient,
queries: [{ indexName: 'indexName' }, { indexName: 'indexName2' }],
});

expect(searchClient.addAlgoliaAgent).toHaveBeenCalledTimes(1);
expect(searchClient.addAlgoliaAgent).toHaveBeenCalledWith(
'autocomplete-core',
Expand All @@ -183,7 +261,7 @@ describe('fetchAlgoliaResults', () => {

await fetchAlgoliaResults({
searchClient,
queries: [],
queries: [{ indexName: 'indexName1' }, { indexName: 'indexName2' }],
userAgents: [{ segment: 'custom-ua', version: '1.0.0' }],
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,14 @@ export function fetchAlgoliaResults<TRecord>({
})
)
.then((response) => {
return response.results.map((result) => ({
return response.results.map((result, resultIndex) => ({
...result,
hits: result.hits?.map((hit) => ({
...hit,
// Bring support for the Insights plugin.
__autocomplete_indexName:
result.index || queries[resultIndex].indexName,
__autocomplete_queryID: result.queryID,
__autocomplete_algoliaCredentials: {
appId,
apiKey,
Expand Down

0 comments on commit 8406eb2

Please sign in to comment.