I am stuck on this ticket: Facet

/**
*

  • @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.

Please look at the error messages where the test case has failed. There must be some error messages if you scroll up on the console.

Kanika

@Kanika
this is the full error log

Determining test suites to run…Setup Mongo Connection
FAIL test/facets.test.js
Facets
✓ should require cast members to perform search with (3ms)
✕ should return movies and runtime/rating facets for returned movies (646ms)
✕ should also support paging (582ms)
✓ should throw an error if castMembers is empty (2ms)

● Facets › should return movies and runtime/rating facets for returned movies

expect(received).toBe(expected) // Object.is equality

Expected: 4
Received: 5

  16 |     expect(actual.movies.length).toBe(20)
  17 |     expect(actual.rating.length).toBe(5)
> 18 |     expect(actual.runtime.length).toBe(4)
     |                                   ^
  19 |     expect(actual.count).toBe(37)
  20 |   })
  21 | 

  at toBe (test/facets.test.js:18:35)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
  at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
  at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

● Facets › should also support paging

expect(received).toBe(expected) // Object.is equality

Expected: 4
Received: 3

  25 |     expect(actual.movies.length).toBe(18)
  26 |     expect(actual.rating.length).toBe(3)
> 27 |     expect(actual.runtime.length).toBe(4)
     |                                   ^
  28 |     expect(actual.count).toBe(38)
  29 |   })
  30 | 

  at toBe (test/facets.test.js:27:35)
  at tryCatch (node_modules/regenerator-runtime/runtime.js:62:40)
  at Generator.invoke [as _invoke] (node_modules/regenerator-runtime/runtime.js:296:22)
  at Generator.prototype.(anonymous function) [as next] (node_modules/regenerator-runtime/runtime.js:114:21)
  at step (node_modules/babel-runtime/helpers/asyncToGenerator.js:17:30)
  at node_modules/babel-runtime/helpers/asyncToGenerator.js:28:13

Test 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.

@Kanika any suggestion what to do?

This message means the expected number is 4 but through your code it is receiving 5. I would have cross-checked the code for both the test cases.

Easy way is look at the test cases, then prepare your query in mongo shell/compass, then write the same in your code.

Hope it helps!

Kanika

@Kanika i have tried but still it is not working , can you be help me check to see where i am getting it wrong

Hi @Clement_Ohiomoje_26072,

Please try after changing the sort stage to tomatoes.viewer.numReviews.

Kanika

Thanks a lot now it is working

1 Like

@Kanika What does the -1 imply?

In sort query, -1 means the sort direction is backward meaning DESCENDING.

Kanika

1 Like