Search and or and not

This commit is contained in:
Víctor Losada Hernández
2026-10-30 15:35:41 +01:00
parent 909d5e4eae
commit 124d03f8c1
3 changed files with 207 additions and 98 deletions
+32 -10
View File
@@ -29,14 +29,30 @@ const rendererConditions = (legacy, v3)=>{
return {}; // If all renderers selected, renderer field not needed in query for speed
};
const tagConditions = (tags) => {
if (!tags || tags.length === 0) return {};
const tagConditions = (tagsAnd, tagsOr, tagsNot) => {
const conditions = {};
return {
$or: tags.map(tag => ({
console.log(tagsAnd.length, tagsOr.length, tagsNot.length);
if (tagsAnd.length > 0) {
conditions.$and = tagsAnd.map(tag => ({
tags: tag.trim()
}))
};
}));
}
if (tagsOr.length > 0) {
conditions.$or = tagsOr.map(tag => ({
tags: tag.trim()
}));
}
if (tagsNot.length > 0) {
conditions.$nor = tagsNot.map(tag => ({
tags: tag.trim()
}));
}
return conditions;
};
const sortConditions = (sort, dir) => {
@@ -51,16 +67,20 @@ const findBrews = async (req, res)=>{
const page = Math.max(parseInt(req.query.page) || 1, 1);
const count = Math.max(parseInt(req.query.count) || 20, 10);
const skip = (page - 1) * count;
const tags = req.query.tags.split(',') || [];
const tagsAnd = req.query.tagsAnd?.split(',') || [];
const tagsOr = req.query.tagsOr?.split(',') || [];
const tagsNot = req.query.tagsNot?.split(',') || [];
const sort = req.query.sort || 'title';
const dir = req.query.dir || 'asc';
console.log(`Title: ${title}, Author: ${author}, page: ${page}, count: ${count}, tagsAnd: ${tagsAnd}, tagsOr: ${tagsOr} tagsNot: ${tagsNot}`);
const combinedQuery = {
$and : [
{ published: true },
rendererConditions(req.query.legacy, req.query.v3),
titleConditions(title),
tagConditions(tags),
tagConditions(tagsAnd, tagsOr, tagsNot),
authorConditions(author)
],
};
@@ -97,7 +117,9 @@ const findBrews = async (req, res)=>{
const findTotal = async (req, res)=>{
const title = req.query.title || '';
const author = req.query.author || '';
const tags = req.query.tags.split(',') || [];
const tagsAnd = req.query.tagsAnd?.split(',') || [];
const tagsOr = req.query.tagsOr?.split(',') || [];
const tagsNot = req.query.tagsNot?.split(',') || [];
const combinedQuery = {
$and : [
@@ -105,7 +127,7 @@ const findTotal = async (req, res)=>{
rendererConditions(req.query.legacy, req.query.v3),
titleConditions(title),
authorConditions(author),
tagConditions(tags)
tagConditions(tagsAnd, tagsOr, tagsNot)
],
};