/**
*
- @param {Object} filters - The search parameter to use in the query. Comes
- in the form of
{cast: { $in: [...castMembers]}}
- @param {number} page - The page of movies to retrieve.
- @param {number} moviesPerPage - The number of movies to display per page.
-
@returns {FacetedSearchReturn} FacetedSearchReturn
*/
static async facetedSearch({
filters = null,
page = 0,
moviesPerPage = 20,
} = {}) {
if (!filters || !filters.cast) {
throw new Error(“Must specify cast members to filter by.”)
}
const matchStage = { $match: filters }
const sortStage = { $sort: { “tomatoes.viewer.rating”: -1 } }
const countingPipeline = [matchStage, sortStage, { $count: “count” }]
const skipStage = { $skip: moviesPerPage * page }
const limitStage = { $limit: moviesPerPage }
const facetStage = {
$facet: {
runtime: [
{
$bucket: {
groupBy: “$runtime”,
boundaries: [0, 60, 90, 120, 180],
default: “other”,
output: {
count: { $sum: 1 },
},
},
},
],
rating: [
{
$bucket: {
groupBy: “$metacritic”,
boundaries: [0, 50, 70, 90, 100],
default: “other”,
output: {
count: { $sum: 1 },
},
},
},
],
movies: [
{
$addFields: {
title: “$title”,
},
},
],
},
}
/**
Ticket: Faceted Search
Please append the skipStage, limitStage, and facetStage to the queryPipeline
(in that order). You can accomplish this by adding the stages directly to
the queryPipeline.
The queryPipeline is a Javascript array, so you can use push() or concat()
to complete this task, but you might have to do something about `const`.
*/
const queryPipeline = [
matchStage,
sortStage,
skipStage,
limitStage,
facetStage
// TODO Ticket: Faceted Search
// Add the stages to queryPipeline in the correct order.
]
try {
const results = await (await movies.aggregate(queryPipeline)).next()
const count = await (await movies.aggregate(countingPipeline)).next()
return {
...results,
...count,
}
} catch (e) {
return { error: "Results too large, be more restrictive in filter" }
}
}
and when i run the test i get the following
est Suites: 1 failed, 1 total
Tests: 2 failed, 2 passed, 4 total
Snapshots: 0 total
Time: 3.214s, estimated 30s
Ran all test suites matching /facets/i.
Teardown Mongo Connection
npm ERR! Test failed. See above for more details.