From e4e4f25864a1f4b2bfdd1c0279f4cf1d3fd51ac1 Mon Sep 17 00:00:00 2001 From: AmasiaNalbandian Date: Thu, 16 Dec 2021 12:15:21 -0500 Subject: [PATCH] changed query fields and added if statements for options passed --- src/api/search/src/bin/validation.js | 13 ++-- src/api/search/src/routes/query.js | 5 +- src/api/search/src/search.js | 104 +++++++++++++++------------ 3 files changed, 65 insertions(+), 57 deletions(-) diff --git a/src/api/search/src/bin/validation.js b/src/api/search/src/bin/validation.js index c159b53849..f34cef732c 100644 --- a/src/api/search/src/bin/validation.js +++ b/src/api/search/src/bin/validation.js @@ -37,21 +37,21 @@ const queryValidationRules = [ */ const advancedQueryValidationRules = [ oneOf([ - check('postText') + check('post') .exists({ checkFalsy: true }) .withMessage('post should not be empty') .bail() .isLength({ max: 256, min: 1 }) .withMessage('post should be between 1 to 256 characters') .bail(), - check('authorText') + check('author') .exists({ checkFalsy: true }) .withMessage('author should exist') .bail() .isLength({ max: 100, min: 2 }) .withMessage('invalid author value') .bail(), - check('titleText') + check('title') .exists({ checkFalsy: true }) .withMessage('title should exist') .bail() @@ -59,9 +59,9 @@ const advancedQueryValidationRules = [ .withMessage('invalid title value') .bail(), ]), - check('dateEnd').optional().isISO8601().withMessage('invalid date format').bail(), + check('to').optional().isISO8601().withMessage('invalid date format').bail(), - check('dateStart').optional().isISO8601().withMessage('invalid date format').bail(), + check('from').optional().isISO8601().withMessage('invalid date format').bail(), check('perPage') .optional() .isInt({ min: 1, max: 10 }) @@ -82,8 +82,7 @@ const advancedQueryValidationRules = [ */ const validateQuery = () => { return async (req, res, next) => { - const rules = - req._parsedUrl.pathname === '/' ? queryValidationRules : advancedQueryValidationRules; + const rules = req.baseUrl === '/' ? queryValidationRules : advancedQueryValidationRules; await Promise.all(rules.map((rule) => rule.run(req))); diff --git a/src/api/search/src/routes/query.js b/src/api/search/src/routes/query.js index 985a01343b..c713afaca8 100644 --- a/src/api/search/src/routes/query.js +++ b/src/api/search/src/routes/query.js @@ -16,10 +16,7 @@ router.get('/', validateQuery, async (req, res, next) => { // route for advanced router.get('/advanced', validateQuery, async (req, res, next) => { try { - const { postText, authorText, titleText, dateStart, dateEnd, page, perPage } = req.query; - res.send( - await advancedSearch(postText, authorText, titleText, dateStart, dateEnd, page, perPage) - ); + res.send(await advancedSearch(req.query)); } catch (error) { next(createError(503, error)); } diff --git a/src/api/search/src/search.js b/src/api/search/src/search.js index ab20b1f972..840c90055b 100644 --- a/src/api/search/src/search.js +++ b/src/api/search/src/search.js @@ -85,63 +85,75 @@ const search = async ( * @param dateEnd - published before this date * @return all the results matching the fields text * Range queries: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#_ranges - * Match field queries: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html#query-dsl-match-query-zero + * Match field queries: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-ppmatch-query.html#query-dsl-match-query-zero */ -const advancedSearch = async ( - postText = '', - authorText = '', - titleText = '', - dateStart = '2000-01-01', - dateEnd = 'now', - page = 0, - perPage = ELASTIC_MAX_RESULTS_PER_PAGE -) => { +const advancedSearch = async (options) => { const query = { query: { bool: { - must: [ - { - match: { - author: { - query: authorText, - zero_terms_query: 'all', - }, - }, - }, - { - match: { - text: { - query: postText, - zero_terms_query: 'all', - }, - }, - }, - { - match: { - title: { - query: titleText, - zero_terms_query: 'all', - }, - }, - }, - { - range: { - published: { - gte: dateStart, - lte: dateEnd, - }, - }, - }, - ], + must: [], }, }, }; + const must = query.query.bool.must; + + if (options.author) { + must.push({ + match: { + author: { + query: options.author, + zero_terms_query: 'all', + }, + }, + }); + } + + if (options.post) { + must.push({ + match: { + text: { + query: options.post, + zero_terms_query: 'all', + }, + }, + }); + } + + if (options.title) { + must.push({ + match: { + title: { + query: options.title, + zero_terms_query: 'all', + }, + }, + }); + } + + if (options.from || options.to) { + must.push({ + range: { + published: { + gte: options.from || '2000-01-01', + lte: options.to || Date.now, + }, + }, + }); + } + + if (!options.perPage) { + options.perPage = ELASTIC_MAX_RESULTS_PER_PAGE; + } + + if (!options.page) { + options.page = 0; + } const { body: { hits }, } = await client.search({ - from: calculateFrom(page, perPage), - size: perPage, + from: calculateFrom(options.page, options.perPage), + size: options.perPage, _source: ['id'], index, type,