-
Notifications
You must be signed in to change notification settings - Fork 223
feat(recommend): Add trending types and methods #1396
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
36bbcfc
feat(recommend): Add trending types and methods
SophieManley03 727c787
fix(recommend): Add a trendingQuery type
SophieManley03 b1f7794
feat(recommend): increase bundle size
SophieManley03 d64940d
fix: remove TrendingType
SophieManley03 4014f29
Merge branch 'feat/recommend-trending' of github.com:algolia/algolias…
SophieManley03 0d304e6
fix: remove items for facet as in the search and use it within trendi…
SophieManley03 3567914
Merge branch 'master' into feat/recommend-trending
shortcuts a1d7636
fix: reintroduce trendingQuery
SophieManley03 cfe8fc5
Merge branch 'feat/recommend-trending' of github.com:algolia/algolias…
SophieManley03 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
packages/recommend/src/__tests__/getTrendingFacets.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { TestSuite } from '../../../client-common/src/__tests__/TestSuite'; | ||
|
||
const recommend = new TestSuite('recommend').recommend; | ||
|
||
function createMockedClient() { | ||
const client = recommend('appId', 'apiKey'); | ||
jest.spyOn(client.transporter, 'read').mockImplementation(() => Promise.resolve()); | ||
|
||
return client; | ||
} | ||
|
||
describe('getTrendingFacets', () => { | ||
test('builds the request', async () => { | ||
const client = createMockedClient(); | ||
|
||
await client.getTrendingFacets( | ||
[ | ||
{ | ||
indexName: 'products', | ||
facetName: 'company', | ||
}, | ||
], | ||
{} | ||
); | ||
|
||
expect(client.transporter.read).toHaveBeenCalledTimes(1); | ||
expect(client.transporter.read).toHaveBeenCalledWith( | ||
{ | ||
cacheable: true, | ||
data: { | ||
requests: [ | ||
{ | ||
indexName: 'products', | ||
model: 'trending-facets', | ||
facetName: 'company', | ||
threshold: 0, | ||
}, | ||
], | ||
}, | ||
method: 'POST', | ||
path: '1/indexes/*/recommendations', | ||
}, | ||
{} | ||
); | ||
}); | ||
}); |
44 changes: 44 additions & 0 deletions
44
packages/recommend/src/__tests__/getTrendingGlobalItems.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { TestSuite } from '../../../client-common/src/__tests__/TestSuite'; | ||
|
||
const recommend = new TestSuite('recommend').recommend; | ||
|
||
function createMockedClient() { | ||
const client = recommend('appId', 'apiKey'); | ||
jest.spyOn(client.transporter, 'read').mockImplementation(() => Promise.resolve()); | ||
|
||
return client; | ||
} | ||
|
||
describe('getTrendingGlobalItems', () => { | ||
test('builds the request', async () => { | ||
const client = createMockedClient(); | ||
|
||
await client.getTrendingGlobalItems( | ||
[ | ||
{ | ||
indexName: 'products', | ||
}, | ||
], | ||
{} | ||
); | ||
|
||
expect(client.transporter.read).toHaveBeenCalledTimes(1); | ||
expect(client.transporter.read).toHaveBeenCalledWith( | ||
{ | ||
cacheable: true, | ||
data: { | ||
requests: [ | ||
{ | ||
indexName: 'products', | ||
model: 'trending-items', | ||
threshold: 0, | ||
}, | ||
], | ||
}, | ||
method: 'POST', | ||
path: '1/indexes/*/recommendations', | ||
}, | ||
{} | ||
); | ||
}); | ||
}); |
48 changes: 48 additions & 0 deletions
48
packages/recommend/src/__tests__/getTrendingItemsForFacet.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { TestSuite } from '../../../client-common/src/__tests__/TestSuite'; | ||
|
||
const recommend = new TestSuite('recommend').recommend; | ||
|
||
function createMockedClient() { | ||
const client = recommend('appId', 'apiKey'); | ||
jest.spyOn(client.transporter, 'read').mockImplementation(() => Promise.resolve()); | ||
|
||
return client; | ||
} | ||
|
||
describe('getTrendingItemsForFacet', () => { | ||
test('builds the request', async () => { | ||
const client = createMockedClient(); | ||
|
||
await client.getTrendingItemsForFacet( | ||
[ | ||
{ | ||
indexName: 'products', | ||
facetName: 'company', | ||
facetValue: 'tesla', | ||
}, | ||
], | ||
{} | ||
); | ||
|
||
expect(client.transporter.read).toHaveBeenCalledTimes(1); | ||
expect(client.transporter.read).toHaveBeenCalledWith( | ||
{ | ||
cacheable: true, | ||
data: { | ||
requests: [ | ||
{ | ||
indexName: 'products', | ||
model: 'trending-items', | ||
facetName: 'company', | ||
facetValue: 'tesla', | ||
threshold: 0, | ||
}, | ||
], | ||
}, | ||
method: 'POST', | ||
path: '1/indexes/*/recommendations', | ||
}, | ||
{} | ||
); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { BaseRecommendClient, TrendingFacetsQuery, WithRecommendMethods } from '../types'; | ||
import { getRecommendations } from './getRecommendations'; | ||
|
||
type GetTrendingFacets = ( | ||
base: BaseRecommendClient | ||
) => WithRecommendMethods<BaseRecommendClient>['getTrendingFacets']; | ||
|
||
export const getTrendingFacets: GetTrendingFacets = base => { | ||
return (queries: readonly TrendingFacetsQuery[], requestOptions) => { | ||
return getRecommendations(base)( | ||
queries.map(query => ({ | ||
...query, | ||
model: 'trending-facets', | ||
})), | ||
requestOptions | ||
); | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { BaseRecommendClient, TrendingGlobalItemsQuery, WithRecommendMethods } from '../types'; | ||
import { getRecommendations } from './getRecommendations'; | ||
|
||
type GetTrendingGlobalItems = ( | ||
base: BaseRecommendClient | ||
) => WithRecommendMethods<BaseRecommendClient>['getTrendingGlobalItems']; | ||
|
||
export const getTrendingGlobalItems: GetTrendingGlobalItems = base => { | ||
return (queries: readonly TrendingGlobalItemsQuery[], requestOptions) => { | ||
return getRecommendations(base)( | ||
queries.map(query => ({ | ||
...query, | ||
model: 'trending-items', | ||
})), | ||
requestOptions | ||
); | ||
}; | ||
}; |
18 changes: 18 additions & 0 deletions
18
packages/recommend/src/methods/getTrendingItemsForFacet.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { BaseRecommendClient, TrendingItemsForFacetQuery, WithRecommendMethods } from '../types'; | ||
import { getRecommendations } from './getRecommendations'; | ||
|
||
type GetTrendingItemsForFacet = ( | ||
base: BaseRecommendClient | ||
) => WithRecommendMethods<BaseRecommendClient>['getTrendingItemsForFacet']; | ||
|
||
export const getTrendingItemsForFacet: GetTrendingItemsForFacet = base => { | ||
return (queries: readonly TrendingItemsForFacetQuery[], requestOptions) => { | ||
return getRecommendations(base)( | ||
queries.map(query => ({ | ||
...query, | ||
model: 'trending-items', | ||
})), | ||
requestOptions | ||
); | ||
}; | ||
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,5 @@ | ||
export type RecommendModel = 'related-products' | 'bought-together'; | ||
export type RecommendModel = | ||
| 'related-products' | ||
| 'bought-together' | ||
| 'trending-items' | ||
| 'trending-facets'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
import { RecommendationsQuery } from './RecommendationsQuery'; | ||
|
||
export type RelatedProductsQuery = Omit<RecommendationsQuery, 'model'>; | ||
export type RelatedProductsQuery = Omit<RecommendationsQuery, 'model' | 'facetName' | 'facetValue'>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { RecommendationsQuery } from './RecommendationsQuery'; | ||
|
||
export type TrendingFacetsQuery = Omit<RecommendationsQuery, 'model' | 'objectID' | 'facetValue'>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { RecommendationsQuery } from './RecommendationsQuery'; | ||
|
||
export type TrendingGlobalItemsQuery = Omit< | ||
RecommendationsQuery, | ||
'model' | 'objectID' | 'facetName' | 'facetValue' | ||
>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { RecommendationsQuery } from './RecommendationsQuery'; | ||
|
||
export type TrendingItemsForFacetQuery = Omit<RecommendationsQuery, 'model' | 'objectID'>; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't want to break the existing implementation for related-products and bought-together. So I put it optional but it is still mandatory for these models :/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it should be required, but omitted from the new items then probably, as how facetName is omitted for fbt
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is omitted actually, how come it's still needed?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the problem if I do that is with getRecommendations . getRecommendation is used for every get method of Recommend and the objectID will be mandatory even for trends
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we still remodel the code so that types are accurate?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So after a third thought, I wanted to try to make it optional here but required for RP and FBT using
Required
in their own Query type.I did that to be able to use
getRecommendations
for all models.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Which bring me to a question after a discussion with @PLNech:
What is the purpose of
getRecommendation
? Does it make sense to make it retrieve recommendations for all kind of model? @francoischalifour maybe you'll know?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Put differently: was
getRecommendation
a hack while we defined the actual model names before GA, or is it a feature designed to allow using not-yet-public new models?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getRecommendations()
is useful for the situation we're in right now: users can already use the trending model, without it being officially supported with a method in the client.We didn't have a team to maintain this Recommend client initially so we provided it as an "escape hatch" if we're not reactive to support new models in the client.
getRecommendations()
was just the very initial implementation that satisfied us at the time, feel free to change it as you need. And if it's not helpful for the new model, don't feel like you need to use it. (In the future, we can also deprecate it.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll ask the opposite question then: what's the purpose of having a specialized method for each model rather than just using
getRecommendations()
? It's basically a multi-query endpoint, and I think it has its uses (like making a single call to retrieve both trending items and trending facets for example). While callinggetRelatedProducts()
as a multi-query is strange IMO.