diff --git a/bundlesize.config.json b/bundlesize.config.json index 5f3b9178..78e43cbc 100644 --- a/bundlesize.config.json +++ b/bundlesize.config.json @@ -2,15 +2,15 @@ "files": [ { "path": "packages/recommend-core/dist/umd/index.js", - "maxSize": "2.05 kB" + "maxSize": "2.25 kB" }, { "path": "packages/recommend-js/dist/umd/index.js", - "maxSize": "7.50 kB" + "maxSize": "7.68 kB" }, { "path": "packages/recommend-react/dist/umd/index.js", - "maxSize": "4.55 kB" + "maxSize": "4.65 kB" }, { "path": "packages/recommend-vdom/dist/umd/index.js", @@ -18,11 +18,11 @@ }, { "path": "packages/recommend-react/dist/umd/experimental-personalization/index.js", - "maxSize": "5.76 kB" + "maxSize": "5.98 kB" }, { "path": "packages/recommend-js/dist/umd/experimental-personalization/index.js", - "maxSize": "8.45 kB" + "maxSize": "8.63 kB" } ] } diff --git a/examples/demo/package.json b/examples/demo/package.json index f314ada9..8e9a549f 100644 --- a/examples/demo/package.json +++ b/examples/demo/package.json @@ -21,7 +21,7 @@ "react": "17.0.2", "react-dom": "17.0.2", "react-router-dom": "^6.17.0", - "search-insights": "2.4.0" + "search-insights": "2.17.3" }, "devDependencies": { "@babel/core": "7.21.4", diff --git a/examples/demo/src/components/ComparisonChartItem/index.tsx b/examples/demo/src/components/ComparisonChartItem/index.tsx index 0c7ee401..d4d5cb11 100644 --- a/examples/demo/src/components/ComparisonChartItem/index.tsx +++ b/examples/demo/src/components/ComparisonChartItem/index.tsx @@ -2,7 +2,6 @@ import React from 'react'; import './style.css'; import { InsightsClient } from 'search-insights'; -import { indexName } from '../../config'; import { ProductHit, ProductReviews } from '../../types'; import { ButtonComponent } from '../common'; @@ -56,10 +55,12 @@ export const ComparisonChartItem: React.FC> = ({ event.preventDefault(); onSelect(item); - insights('clickedObjectIDs', { + insights('clickedObjectIDsAfterSearch', { objectIDs: [item.objectID], eventName: 'Product Clicked', - index: indexName, + index: item.__indexName, + positions: [item.__position], + queryID: item.__queryID, }); }} > @@ -78,7 +79,7 @@ export const ComparisonChartItem: React.FC> = ({ insights('convertedObjectIDsAfterSearch', { eventName: 'Product Added To Cart', objectIDs: [item.objectID], - index: indexName, + index: item.__indexName, queryID: item.__queryID, }); }} diff --git a/examples/demo/src/components/Facet/index.tsx b/examples/demo/src/components/Facet/index.tsx index 879c386e..33305ce3 100644 --- a/examples/demo/src/components/Facet/index.tsx +++ b/examples/demo/src/components/Facet/index.tsx @@ -1,29 +1,21 @@ import { TrendingFacetHit } from '@algolia/recommend'; import React from 'react'; -import { InsightsClient } from 'search-insights'; import './style.css'; type FacetProps = { hit: TrendingFacetHit; indexName: string; - insights: InsightsClient; onSelect(hit: TrendingFacetHit): void; }; -export function Facet({ hit, onSelect, indexName, insights }: FacetProps) { +export function Facet({ hit, onSelect }: FacetProps) { return (
{ event.preventDefault(); - onSelect(hit); - insights('clickedFilters', { - index: indexName, - eventName: 'Facet Clicked', - filters: [`${hit.facetName}:${hit.facetValue}`], - }); }} >
diff --git a/examples/demo/src/components/Hit/index.tsx b/examples/demo/src/components/Hit/index.tsx index 3af01182..49d91c24 100644 --- a/examples/demo/src/components/Hit/index.tsx +++ b/examples/demo/src/components/Hit/index.tsx @@ -1,7 +1,6 @@ import React from 'react'; import { InsightsClient } from 'search-insights'; -import { indexName } from '../../config'; import { ProductHit } from '../../types'; import './style.css'; import { ButtonComponent } from '../common'; @@ -21,10 +20,12 @@ export function Hit({ hit, onSelect, insights }: HitProps) { event.preventDefault(); onSelect(hit); - insights('clickedObjectIDs', { + insights('clickedObjectIDsAfterSearch', { objectIDs: [hit.objectID], eventName: 'Product Clicked', - index: indexName, + index: hit.__indexName, + positions: [hit.__position], + queryID: hit.__queryID, }); }} > @@ -45,7 +46,7 @@ export function Hit({ hit, onSelect, insights }: HitProps) { insights('convertedObjectIDsAfterSearch', { eventName: 'Product Added To Cart', objectIDs: [hit.objectID], - index: indexName, + index: hit.__indexName, queryID: hit.__queryID, }); }} diff --git a/examples/demo/src/routes/HomePage.tsx b/examples/demo/src/routes/HomePage.tsx index 273bb991..7965b096 100644 --- a/examples/demo/src/routes/HomePage.tsx +++ b/examples/demo/src/routes/HomePage.tsx @@ -28,7 +28,6 @@ export const HomePage: React.FC = () => { itemComponent={({ item }) => ( { const isSameValue = selectedFacetValue && @@ -70,6 +69,9 @@ export const HomePage: React.FC = () => { selectedFacetValue ? `in ${selectedFacetValue.facetValue}` : '' }`, }} + queryParameters={{ + clickAnalytics: true, + }} /> recommendClient={recommendClient} @@ -77,6 +79,7 @@ export const HomePage: React.FC = () => { maxRecommendations={15} queryParameters={{ userToken: 'likes-gender-men', + clickAnalytics: true, }} itemComponent={({ item }) => ( = THit & { - __position: string; + __position: number; __indexName: string; __queryID: string; }; diff --git a/examples/js-demo/RelatedItem.tsx b/examples/js-demo/RelatedItem.tsx index cb277f8a..ae80447f 100644 --- a/examples/js-demo/RelatedItem.tsx +++ b/examples/js-demo/RelatedItem.tsx @@ -12,10 +12,12 @@ export function RelatedItem({ item, insights, onSelect }: RelatedItemProps) { onClick={(event) => { event.preventDefault(); onSelect(item); - insights('clickedObjectIDs', { + insights('clickedObjectIDsAfterSearch', { objectIDs: [item.objectID], eventName: 'Product Clicked', index: item.__indexName, + positions: [item.__position], + queryID: item.__queryID, }); }} > diff --git a/examples/js-demo/package.json b/examples/js-demo/package.json index c210a3ad..370ebf84 100644 --- a/examples/js-demo/package.json +++ b/examples/js-demo/package.json @@ -21,7 +21,7 @@ "@babel/runtime": "7.21.0", "algoliasearch": "4.17.0", "preact": "10.13.2", - "search-insights": "2.4.0" + "search-insights": "2.17.3" }, "devDependencies": { "@babel/core": "7.21.4", diff --git a/examples/js-demo/types/ProductHit.ts b/examples/js-demo/types/ProductHit.ts index a83030df..085f9174 100644 --- a/examples/js-demo/types/ProductHit.ts +++ b/examples/js-demo/types/ProductHit.ts @@ -29,7 +29,7 @@ export type ProductRecord = { }; type WithInsights = THit & { - __position: string; + __position: number; __indexName: string; __queryID: string; }; diff --git a/packages/recommend-core/src/__tests__/getBatchRecommendations.test.ts b/packages/recommend-core/src/__tests__/getBatchRecommendations.test.ts index 40415c42..8d484a21 100644 --- a/packages/recommend-core/src/__tests__/getBatchRecommendations.test.ts +++ b/packages/recommend-core/src/__tests__/getBatchRecommendations.test.ts @@ -33,7 +33,7 @@ describe('getBatchRecommendations', () => { expect(recommendClient.getRecommendations).toHaveBeenCalledTimes(1); expect(Object.keys(result)[0]).toEqual('{"key":"key-1"}'); expect(Object.values(result)[0]).toEqual({ - recommendations: [hit], + recommendations: [{ ...hit, __indexName: 'indexName', __position: 1 }], trendingFacets: [], }); }); diff --git a/packages/recommend-core/src/__tests__/getRecommendations.test.ts b/packages/recommend-core/src/__tests__/getRecommendations.test.ts index 822730f1..35009b24 100644 --- a/packages/recommend-core/src/__tests__/getRecommendations.test.ts +++ b/packages/recommend-core/src/__tests__/getRecommendations.test.ts @@ -114,7 +114,7 @@ describe('getRecommendations', () => { getRecommendations: jest.fn(() => Promise.resolve( createMultiSearchResponse({ - hits: [hit], + hits: [{ ...hit, __indexName: 'indexName', __position: 1 }], }) ) ), @@ -129,6 +129,8 @@ describe('getRecommendations', () => { expect(recommendations).toEqual([ { + __indexName: 'indexName', + __position: 1, category: 'Women - Jumpsuits-Overalls', hierarchical_categories: { lvl0: 'women', diff --git a/packages/recommend-core/src/getBatchRecommendations.ts b/packages/recommend-core/src/getBatchRecommendations.ts index edffe204..c0925b25 100644 --- a/packages/recommend-core/src/getBatchRecommendations.ts +++ b/packages/recommend-core/src/getBatchRecommendations.ts @@ -7,6 +7,8 @@ import { import { TrendingFacetHit, ProductRecord } from './types'; import { mapByScoreToRecommendations, mapToRecommendations } from './utils'; +import { addAbsolutePosition } from './utils/addAbsolutePosition'; +import { addIndexName } from './utils/addIndexName'; import { version } from './version'; export type BatchKeyPair = { @@ -55,9 +57,11 @@ export async function getBatchRecommendations({ const { model } = JSON.parse(keyPair.key); allChunks += keyPair.value; - const { maxRecommendations, transformItems = (x) => x } = queries[ - prevChunks - ]; + const { + indexName, + maxRecommendations, + transformItems = (x) => x, + } = queries[prevChunks]; const splitResult = response?.results?.slice(prevChunks, allChunks); prevChunks += keyPair.value; @@ -71,13 +75,18 @@ export async function getBatchRecommendations({ recommendations = mapByScoreToRecommendations>({ maxRecommendations, hits: splitResult.map((res) => res.hits).flat(), + indexName, + queryID: splitResult.at(0)?.queryID, }); } else { recommendations = mapToRecommendations>({ maxRecommendations, hits: splitResult.map((res) => res.hits), nrOfObjs: keyPair.value, - }); + queryIDs: splitResult.map((res) => res.queryID), + }) + .map((hit) => addIndexName(hit, indexName)) + .map((hit, idx) => addAbsolutePosition(hit, idx)); } recommendations = transformItems(recommendations); results[keyPair.key] = { recommendations, trendingFacets }; diff --git a/packages/recommend-core/src/getFrequentlyBoughtTogether.ts b/packages/recommend-core/src/getFrequentlyBoughtTogether.ts index 6b7f28cc..f5a22f9e 100644 --- a/packages/recommend-core/src/getFrequentlyBoughtTogether.ts +++ b/packages/recommend-core/src/getFrequentlyBoughtTogether.ts @@ -3,6 +3,8 @@ import { FrequentlyBoughtTogetherQuery } from '@algolia/recommend'; import { RecommendationsProps } from './getRecommendations'; import { ProductRecord } from './types'; import { mapToRecommendations } from './utils'; +import { addAbsolutePosition } from './utils/addAbsolutePosition'; +import { addIndexName } from './utils/addIndexName'; import { version } from './version'; export type GetFrequentlyBoughtTogetherProps< @@ -40,7 +42,13 @@ export function getFrequentlyBoughtTogether({ maxRecommendations, hits: response.results.map((result) => result.hits), nrOfObjs: objectIDs.length, + queryIDs: response.results.map((res) => res.queryID), }) ) + .then((hits) => + hits + .map((hit) => addIndexName(hit, indexName)) + .map((hit, idx) => addAbsolutePosition(hit, idx)) + ) .then((hits) => ({ recommendations: transformItems(hits) })); } diff --git a/packages/recommend-core/src/getLookingSimilar.ts b/packages/recommend-core/src/getLookingSimilar.ts index 9866ae31..40ecf5c2 100644 --- a/packages/recommend-core/src/getLookingSimilar.ts +++ b/packages/recommend-core/src/getLookingSimilar.ts @@ -3,6 +3,8 @@ import { LookingSimilarQuery } from '@algolia/recommend'; import { RecommendationsProps } from './getRecommendations'; import { ProductRecord } from './types'; import { mapToRecommendations } from './utils'; +import { addAbsolutePosition } from './utils/addAbsolutePosition'; +import { addIndexName } from './utils/addIndexName'; import { version } from './version'; export type GetLookingSimilarProps = RecommendationsProps & @@ -40,7 +42,13 @@ export function getLookingSimilar({ maxRecommendations, hits: response.results.map((result) => result.hits), nrOfObjs: objectIDs.length, + queryIDs: response.results.map((res) => res.queryID), }) ) + .then((hits) => + hits + .map((hit) => addIndexName(hit, indexName)) + .map((hit, idx) => addAbsolutePosition(hit, idx)) + ) .then((hits) => ({ recommendations: transformItems(hits) })); } diff --git a/packages/recommend-core/src/getRecommendations.ts b/packages/recommend-core/src/getRecommendations.ts index b8d93f03..729a4202 100644 --- a/packages/recommend-core/src/getRecommendations.ts +++ b/packages/recommend-core/src/getRecommendations.ts @@ -2,6 +2,8 @@ import type { RecommendClient, RecommendationsQuery } from '@algolia/recommend'; import { ProductRecord, RecordWithObjectID } from './types'; import { mapToRecommendations } from './utils'; +import { addAbsolutePosition } from './utils/addAbsolutePosition'; +import { addIndexName } from './utils/addIndexName'; import { version } from './version'; export type RecommendationsProps = { @@ -66,7 +68,13 @@ export function getRecommendations({ maxRecommendations, hits: response.results.map((result) => result.hits), nrOfObjs: objectIDs.length, + queryIDs: response.results.map((res) => res.queryID), }) ) + .then((hits) => + hits + .map((hit) => addIndexName(hit, indexName)) + .map((hit, idx) => addAbsolutePosition(hit, idx)) + ) .then((hits) => ({ recommendations: transformItems(hits) })); } diff --git a/packages/recommend-core/src/getRecommendedForYou.ts b/packages/recommend-core/src/getRecommendedForYou.ts index 091f1a8c..66a5c48d 100644 --- a/packages/recommend-core/src/getRecommendedForYou.ts +++ b/packages/recommend-core/src/getRecommendedForYou.ts @@ -1,6 +1,9 @@ import { Hit } from '@algolia/client-search'; import { RecommendClient, RecommendedForYouParams } from '@algolia/recommend'; +import { addAbsolutePosition } from './utils/addAbsolutePosition'; +import { addIndexName } from './utils/addIndexName'; +import { addQueryID } from './utils/addQueryID'; import { version } from './version'; export type GetRecommendedForYouProps = { @@ -36,9 +39,16 @@ export function getRecommendedForYou({ return recommendClient .getRecommendedForYou(queries) - .then((hits) => ({ + .then((response) => ({ recommendations: transformItems( - hits.results.map((result) => result.hits).flat() + response.results + .map((result) => + result.hits + .map((hit) => addIndexName(hit, indexName)) + .map((hit) => addQueryID(hit, result.queryID)) + ) + .flat() + .map((hit, idx) => addAbsolutePosition(hit, idx)) ), })); } diff --git a/packages/recommend-core/src/getRelatedProducts.ts b/packages/recommend-core/src/getRelatedProducts.ts index af6d5957..54c641b6 100644 --- a/packages/recommend-core/src/getRelatedProducts.ts +++ b/packages/recommend-core/src/getRelatedProducts.ts @@ -3,6 +3,8 @@ import { RelatedProductsQuery } from '@algolia/recommend'; import { RecommendationsProps } from './getRecommendations'; import { ProductRecord } from './types'; import { mapToRecommendations } from './utils'; +import { addAbsolutePosition } from './utils/addAbsolutePosition'; +import { addIndexName } from './utils/addIndexName'; import { version } from './version'; export type GetRelatedProductsProps = RecommendationsProps & @@ -39,8 +41,14 @@ export function getRelatedProducts({ mapToRecommendations>({ maxRecommendations, hits: response.results.map((result) => result.hits), + queryIDs: response.results.map((result) => result.queryID), nrOfObjs: objectIDs.length, }) ) + .then((hits) => + hits + .map((hit) => addIndexName(hit, indexName)) + .map((hit, idx) => addAbsolutePosition(hit, idx)) + ) .then((hits) => ({ recommendations: transformItems(hits) })); } diff --git a/packages/recommend-core/src/getTrendingFacets.ts b/packages/recommend-core/src/getTrendingFacets.ts index c48e3471..6d769765 100644 --- a/packages/recommend-core/src/getTrendingFacets.ts +++ b/packages/recommend-core/src/getTrendingFacets.ts @@ -46,6 +46,7 @@ export function getTrendingFacets({ mapByScoreToRecommendations({ maxRecommendations, hits: response.results.map((result) => result.hits).flat(), + indexName, }) ) .then((hits) => ({ recommendations: transformItems(hits) })); diff --git a/packages/recommend-core/src/getTrendingItems.ts b/packages/recommend-core/src/getTrendingItems.ts index d2811e73..3ad6540b 100644 --- a/packages/recommend-core/src/getTrendingItems.ts +++ b/packages/recommend-core/src/getTrendingItems.ts @@ -64,6 +64,8 @@ export function getTrendingItems({ 'objectID', response.results.map((result) => result.hits).flat() ), + indexName, + queryID: response.results?.at(0)?.queryID, }) ) .then((hits) => ({ recommendations: transformItems(hits) })); diff --git a/packages/recommend-core/src/types/ProductRecord.ts b/packages/recommend-core/src/types/ProductRecord.ts index 24c555bd..9d0b1464 100644 --- a/packages/recommend-core/src/types/ProductRecord.ts +++ b/packages/recommend-core/src/types/ProductRecord.ts @@ -1,4 +1,7 @@ export type ProductRecord = TObject & { objectID: string; _score?: number; + __queryID?: string; + __position?: number; + __indexName?: string; }; diff --git a/packages/recommend-core/src/types/RecordWithObjectID.ts b/packages/recommend-core/src/types/RecordWithObjectID.ts index 199bcf86..a0617fa6 100644 --- a/packages/recommend-core/src/types/RecordWithObjectID.ts +++ b/packages/recommend-core/src/types/RecordWithObjectID.ts @@ -1,3 +1,6 @@ export type RecordWithObjectID = TObject & { objectID: string; + __queryID?: string; + __position?: number; + __indexName?: string; }; diff --git a/packages/recommend-core/src/utils/__tests__/mapByScoreToRecommendations.test.ts b/packages/recommend-core/src/utils/__tests__/mapByScoreToRecommendations.test.ts index 33ebf504..3431ca6a 100644 --- a/packages/recommend-core/src/utils/__tests__/mapByScoreToRecommendations.test.ts +++ b/packages/recommend-core/src/utils/__tests__/mapByScoreToRecommendations.test.ts @@ -79,53 +79,85 @@ describe('mapByScoreToRecommendations', () => { 'objectID', response.results.map((result) => result.hits).flat() ), + indexName: 'index-name', + queryID: 'some-random-query-id', }); expect(result).toMatchInlineSnapshot(` Array [ Object { + "__indexName": "index-name", + "__position": 1, + "__queryID": "some-random-query-id", "_score": 100, "name": "Product 1-1", "objectID": "1-1", }, Object { + "__indexName": "index-name", + "__position": 2, + "__queryID": "some-random-query-id", "_score": 100, "name": "Product 2-1", "objectID": "2-1", }, Object { + "__indexName": "index-name", + "__position": 3, + "__queryID": "some-random-query-id", "_score": 90, "name": "Product 1-2", "objectID": "1-2", }, Object { + "__indexName": "index-name", + "__position": 4, + "__queryID": "some-random-query-id", "_score": 90, "name": "Product 2-2", "objectID": "2-2", }, Object { + "__indexName": "index-name", + "__position": 5, + "__queryID": "some-random-query-id", "_score": 70, "name": "Product 1-3", "objectID": "1-3", }, Object { + "__indexName": "index-name", + "__position": 6, + "__queryID": "some-random-query-id", "_score": 70, "name": "Product 2-3", "objectID": "2-3", }, Object { + "__indexName": "index-name", + "__position": 7, + "__queryID": "some-random-query-id", "name": "Product 1-4", "objectID": "1-4", }, Object { + "__indexName": "index-name", + "__position": 8, + "__queryID": "some-random-query-id", "name": "Product 1-5", "objectID": "1-5", }, Object { + "__indexName": "index-name", + "__position": 9, + "__queryID": "some-random-query-id", "name": "Product 2-4", "objectID": "2-4", }, Object { + "__indexName": "index-name", + "__position": 10, + "__queryID": "some-random-query-id", "name": "Product 2-5", "objectID": "2-5", }, diff --git a/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts b/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts index d65d6c11..97604fc9 100644 --- a/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts +++ b/packages/recommend-core/src/utils/__tests__/mapToRecommendations.test.ts @@ -80,21 +80,25 @@ describe('mapToRecommendations', () => { const result = mapToRecommendations({ hits: response.results.map((result) => result.hits), nrOfObjs: 2, + queryIDs: ['queryID1'], }); expect(result).toMatchInlineSnapshot(` Array [ Object { + "__queryID": "queryID1", "_score": 0, "name": "Product B", "objectID": "B", }, Object { + "__queryID": "queryID1", "_score": 76, "name": "Product E", "objectID": "E", }, Object { + "__queryID": "queryID1", "_score": 0, "name": "Product C", "objectID": "C", @@ -105,6 +109,7 @@ describe('mapToRecommendations', () => { "objectID": "F", }, Object { + "__queryID": "queryID1", "_score": 100, "name": "Product A", "objectID": "A", @@ -115,6 +120,7 @@ describe('mapToRecommendations', () => { "objectID": "G", }, Object { + "__queryID": "queryID1", "_score": 89, "name": "Product D", "objectID": "D", diff --git a/packages/recommend-core/src/utils/addAbsolutePosition.ts b/packages/recommend-core/src/utils/addAbsolutePosition.ts new file mode 100644 index 00000000..e671d2f3 --- /dev/null +++ b/packages/recommend-core/src/utils/addAbsolutePosition.ts @@ -0,0 +1,6 @@ +export function addAbsolutePosition(hit: THit, index: number): THit { + return { + ...hit, + __position: index + 1, + }; +} diff --git a/packages/recommend-core/src/utils/addIndexName.ts b/packages/recommend-core/src/utils/addIndexName.ts new file mode 100644 index 00000000..555a2f6a --- /dev/null +++ b/packages/recommend-core/src/utils/addIndexName.ts @@ -0,0 +1,6 @@ +export function addIndexName(hit: THit, indexName: string): THit { + return { + ...hit, + __indexName: indexName, + }; +} diff --git a/packages/recommend-core/src/utils/addQueryID.ts b/packages/recommend-core/src/utils/addQueryID.ts new file mode 100644 index 00000000..905ebd8e --- /dev/null +++ b/packages/recommend-core/src/utils/addQueryID.ts @@ -0,0 +1,10 @@ +export function addQueryID(hit: THit, queryID?: string): THit { + if (!queryID) { + return hit; + } + + return { + ...hit, + __queryID: queryID, + }; +} diff --git a/packages/recommend-core/src/utils/mapByScoreToRecommendations.ts b/packages/recommend-core/src/utils/mapByScoreToRecommendations.ts index 58d5ba50..b87645ca 100644 --- a/packages/recommend-core/src/utils/mapByScoreToRecommendations.ts +++ b/packages/recommend-core/src/utils/mapByScoreToRecommendations.ts @@ -1,15 +1,25 @@ +import { addAbsolutePosition } from './addAbsolutePosition'; +import { addIndexName } from './addIndexName'; +import { addQueryID } from './addQueryID'; import { sortBy } from './sortBy'; type MapToRecommendations = { hits: THit[]; + indexName: string; maxRecommendations?: number; + queryID?: string; }; export function mapByScoreToRecommendations< THit extends { _score?: number; } ->({ hits, maxRecommendations }: MapToRecommendations): THit[] { +>({ + hits, + indexName, + maxRecommendations, + queryID, +}: MapToRecommendations): THit[] { // Since recommendations from multiple indices are returned, we // need to sort them descending based on their score. return sortBy((a, b) => { @@ -17,12 +27,16 @@ export function mapByScoreToRecommendations< const scoreB = b._score || 0; return scoreA > scoreB ? -1 : 1; - }, hits).slice( - 0, - // We cap the number of recommendations because the previously - // computed `hitsPerPage` was an approximation due to `Math.ceil`. - maxRecommendations && maxRecommendations > 0 - ? maxRecommendations - : undefined - ); + }, hits) + .slice( + 0, + // We cap the number of recommendations because the previously + // computed `hitsPerPage` was an approximation due to `Math.ceil`. + maxRecommendations && maxRecommendations > 0 + ? maxRecommendations + : undefined + ) + .map((hit) => addQueryID(hit, queryID)) + .map((hit) => addIndexName(hit, indexName)) + .map((hit, idx) => addAbsolutePosition(hit, idx)); } diff --git a/packages/recommend-core/src/utils/mapToRecommendations.ts b/packages/recommend-core/src/utils/mapToRecommendations.ts index 350259b1..6b9eb6cf 100644 --- a/packages/recommend-core/src/utils/mapToRecommendations.ts +++ b/packages/recommend-core/src/utils/mapToRecommendations.ts @@ -14,6 +14,7 @@ import { getAverageIndices } from './computeAverageIndices'; type MapToRecommendations = { hits: Array>>; + queryIDs: Array; maxRecommendations?: number; nrOfObjs: number; }; @@ -22,11 +23,15 @@ export function mapToRecommendations({ hits, maxRecommendations, nrOfObjs, + queryIDs, }: MapToRecommendations): Array> { const indexTracker: IndexTracker = {}; - hits.forEach((hitsArray) => { + hits.forEach((hitsArray, idx) => { hitsArray.forEach((hit, index) => { + if (queryIDs[idx]) { + hit.__queryID = queryIDs[idx]; + } if (!indexTracker[hit.objectID]) { indexTracker[hit.objectID] = { indexSum: index, nr: 1 }; } else { diff --git a/packages/recommend-react/src/__tests__/useFrequentlyBoughtTogether.test.tsx b/packages/recommend-react/src/__tests__/useFrequentlyBoughtTogether.test.tsx index 6f34dc46..cfcde961 100644 --- a/packages/recommend-react/src/__tests__/useFrequentlyBoughtTogether.test.tsx +++ b/packages/recommend-react/src/__tests__/useFrequentlyBoughtTogether.test.tsx @@ -43,7 +43,9 @@ describe('useFrequentlyBoughtTogether', () => { ); await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); }); diff --git a/packages/recommend-react/src/__tests__/useLookingSimilar.test.tsx b/packages/recommend-react/src/__tests__/useLookingSimilar.test.tsx index baadd6c3..76e20b29 100644 --- a/packages/recommend-react/src/__tests__/useLookingSimilar.test.tsx +++ b/packages/recommend-react/src/__tests__/useLookingSimilar.test.tsx @@ -47,7 +47,9 @@ describe('useLookingSimilar', () => { await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); }); diff --git a/packages/recommend-react/src/__tests__/useRecommendations.test.tsx b/packages/recommend-react/src/__tests__/useRecommendations.test.tsx index 88121ea1..818792ec 100644 --- a/packages/recommend-react/src/__tests__/useRecommendations.test.tsx +++ b/packages/recommend-react/src/__tests__/useRecommendations.test.tsx @@ -48,7 +48,9 @@ describe('useRecommendations', () => { await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); }); diff --git a/packages/recommend-react/src/__tests__/useRecommendedForYou.test.tsx b/packages/recommend-react/src/__tests__/useRecommendedForYou.test.tsx index 3d814990..0c47e13a 100644 --- a/packages/recommend-react/src/__tests__/useRecommendedForYou.test.tsx +++ b/packages/recommend-react/src/__tests__/useRecommendedForYou.test.tsx @@ -47,7 +47,9 @@ describe('useRecommendedForYou', () => { await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); }); diff --git a/packages/recommend-react/src/__tests__/useRelatedProducts.test.tsx b/packages/recommend-react/src/__tests__/useRelatedProducts.test.tsx index 896338ff..1fc7c750 100644 --- a/packages/recommend-react/src/__tests__/useRelatedProducts.test.tsx +++ b/packages/recommend-react/src/__tests__/useRelatedProducts.test.tsx @@ -47,7 +47,9 @@ describe('useRelatedProducts', () => { await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); }); diff --git a/packages/recommend-react/src/__tests__/useTrendingFacets.test.tsx b/packages/recommend-react/src/__tests__/useTrendingFacets.test.tsx index 4ab3f8d2..db612c27 100644 --- a/packages/recommend-react/src/__tests__/useTrendingFacets.test.tsx +++ b/packages/recommend-react/src/__tests__/useTrendingFacets.test.tsx @@ -41,7 +41,9 @@ describe('useTrendingFacets', () => { await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); }); diff --git a/packages/recommend-react/src/__tests__/useTrendingItems.test.tsx b/packages/recommend-react/src/__tests__/useTrendingItems.test.tsx index e23ec7c2..df1c1819 100644 --- a/packages/recommend-react/src/__tests__/useTrendingItems.test.tsx +++ b/packages/recommend-react/src/__tests__/useTrendingItems.test.tsx @@ -48,7 +48,9 @@ describe('useTrendingItems', () => { await waitForNextUpdate(); await waitFor(() => { - expect(result.current.recommendations).toEqual([hit]); + expect(result.current.recommendations).toEqual([ + { ...hit, __indexName: 'test', __position: 1 }, + ]); }); });