Skip to content

Commit

Permalink
changed query fields and added if statements for options passed
Browse files Browse the repository at this point in the history
  • Loading branch information
AmasiaNalbandian committed Dec 16, 2021
1 parent f33158b commit e4e4f25
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 57 deletions.
13 changes: 6 additions & 7 deletions src/api/search/src/bin/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,31 @@ 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()
.isLength({ max: 100, min: 2 })
.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 })
Expand All @@ -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)));

Expand Down
5 changes: 1 addition & 4 deletions src/api/search/src/routes/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
104 changes: 58 additions & 46 deletions src/api/search/src/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit e4e4f25

Please sign in to comment.