Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/algoliasearch-helper/src/algoliasearch.helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -1922,6 +1922,7 @@ AlgoliaSearchHelper.prototype._dispatchRecommendResponse = function (
}
cache[id] = Object.assign({}, firstResult, {
hits: sortAndMergeRecommendations(
ids,
indices.map(function (idx) {
return content.results[idx].hits;
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ function getAverageIndices(indexTracker, nrOfObjs) {
});
}

function sortAndMergeRecommendations(results) {
function sortAndMergeRecommendations(objectIDs, results) {
var indexTracker = {};

results.forEach(function (hits) {
hits.forEach(function (hit, index) {
if (objectIDs.includes(hit.objectID)) return;

if (!indexTracker[hit.objectID]) {
indexTracker[hit.objectID] = { indexSum: index, count: 1 };
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ var response = {

test('sorts the items based on their average index thus preserving applied rules', () => {
const result = sortAndMergeRecommendations(
['X', 'Y', 'Z'],
response.results.map(function (res) {
return res.hits;
})
Expand Down Expand Up @@ -124,3 +125,37 @@ test('sorts the items based on their average index thus preserving applied rules
]
`);
});

test('filters out input objectIDs', () => {
const result = sortAndMergeRecommendations(
['A', 'B', 'C'],
response.results.map(function (res) {
return res.hits;
})
);

expect(result).toMatchInlineSnapshot(`
Array [
Object {
"_score": 76,
"name": "Product E",
"objectID": "E",
},
Object {
"_score": 100,
"name": "Product F",
"objectID": "F",
},
Object {
"_score": 96,
"name": "Product G",
"objectID": "G",
},
Object {
"_score": 89,
"name": "Product D",
"objectID": "D",
},
]
`);
});