forked from lukePeavey/quotable
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: search quotes (lukePeavey#30)
- Loading branch information
Mitra Mejia
authored
Aug 31, 2020
1 parent
fdaa052
commit 535c11c
Showing
6 changed files
with
111 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,62 @@ | ||
const clamp = require('lodash/clamp') | ||
const omit = require('lodash/omit') | ||
const createError = require('http-errors') | ||
const Quotes = require('../../models/Quotes') | ||
|
||
/** | ||
* Search for quotes by content and author | ||
* | ||
* @param {Object} params | ||
* @param {Object} params.query The search query | ||
* @param {number} [params.limit = 20] The maximum number of results to include | ||
* in a single response. | ||
* @param {number} [params.skip = 0] The offset for pagination. | ||
*/ | ||
module.exports = async function searchQuotes(req, res, next) { | ||
try { | ||
const { query = '' } = req.query | ||
let { limit = 20, skip = 0 } = req.query | ||
|
||
// Use a $text search query to search `content` and `author` fields | ||
// @see https://docs.mongodb.com/manual/reference/operator/query/text | ||
const filter = { | ||
$text: { $search: query }, | ||
} | ||
|
||
// Add a `score` field that will be used to sort results by text score. | ||
// @see https://docs.mongodb.com/manual/reference/operator/projection/meta | ||
const projection = { | ||
score: { $meta: 'textScore' }, | ||
} | ||
|
||
// Sorting and pagination params | ||
limit = clamp(parseInt(limit), 0, 50) || 20 | ||
skip = parseInt(skip) || 0 | ||
|
||
const [results, totalCount] = await Promise.all([ | ||
Quotes.find(filter, projection) | ||
.sort({ score: { $meta: 'textScore' } }) | ||
.skip(skip) | ||
.limit(limit) | ||
.select('-__v -authorId'), | ||
|
||
Quotes.countDocuments(filter), | ||
]) | ||
|
||
// `lastItemIndex` is the offset of the last result returned by this | ||
// request. When paginating through results, this would be used as the | ||
// `skip` parameter when requesting the next page of results. It will be | ||
// set to `null` if there are no additional results. | ||
const lastItemIndex = skip + results.length | ||
|
||
// Return a paginated list of quotes to client | ||
res.status(200).json({ | ||
count: results.length, | ||
totalCount, | ||
lastItemIndex: lastItemIndex >= totalCount ? null : lastItemIndex, | ||
results: results.map(doc => omit(doc.toJSON(), 'score')), | ||
}) | ||
} catch (error) { | ||
return next(error) | ||
} | ||
} |
This file contains 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