Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(recommend): only sort items with scores #42

Merged
merged 1 commit into from
Jun 29, 2021
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import { mapToRecommendations } from '../mapToRecommendations';

const response = {
results: [
{
exhaustiveNbHits: true,
hits: [
{
objectID: '1-1',
name: 'Product 1-1',
_score: 100,
},
{
objectID: '1-2',
name: 'Product 1-2',
_score: 90,
},
{
objectID: '1-3',
name: 'Product 1-3',
_score: 70,
},
{
objectID: '1-4',
name: 'Product 1-4',
},
{
objectID: '1-5',
name: 'Product 1-5',
},
],
hitsPerPage: 10,
nbHits: 10,
nbPages: 1,
page: 0,
processingTimeMS: 1,
},
{
exhaustiveNbHits: true,
hits: [
{
objectID: '2-1',
name: 'Product 2-1',
_score: 100,
},
{
objectID: '2-2',
name: 'Product 2-2',
_score: 90,
},
{
objectID: '2-3',
name: 'Product 2-3',
_score: 70,
},
{
objectID: '2-4',
name: 'Product 2-4',
},
{
objectID: '2-5',
name: 'Product 2-5',
},
],
hitsPerPage: 10,
nbHits: 10,
nbPages: 1,
page: 0,
processingTimeMS: 1,
},
],
};

describe('mapToRecommendations', () => {
francoischalifour marked this conversation as resolved.
Show resolved Hide resolved
test('sorts the items based on their scores', () => {
const result = mapToRecommendations({ response: response as any });

expect(result).toMatchInlineSnapshot(`
Array [
Object {
"_score": 100,
"name": "Product 1-1",
"objectID": "1-1",
},
Object {
"_score": 100,
"name": "Product 2-1",
"objectID": "2-1",
},
Object {
"_score": 90,
"name": "Product 1-2",
"objectID": "1-2",
},
Object {
"_score": 90,
"name": "Product 2-2",
"objectID": "2-2",
},
Object {
"_score": 70,
"name": "Product 1-3",
"objectID": "1-3",
},
Object {
"_score": 70,
"name": "Product 2-3",
"objectID": "2-3",
},
Object {
"name": "Product 1-4",
"objectID": "1-4",
},
Object {
"name": "Product 1-5",
"objectID": "1-5",
},
Object {
"name": "Product 2-4",
"objectID": "2-4",
},
Object {
"name": "Product 2-5",
"objectID": "2-5",
},
]
`);
});
});
2 changes: 1 addition & 1 deletion packages/recommend-core/src/utils/mapToRecommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function mapToRecommendations<TObject>({
const scoreA = a._score || 0;
const scoreB = b._score || 0;

return scoreA < scoreB ? 1 : -1;
return scoreA > scoreB ? -1 : 1;
},
// Multiple identical recommended `objectID`s can be returned b
// the engine, so we need to remove duplicates.
Expand Down