mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-15 12:28:45 +00:00
only use tag index if only tags are present and not text
This commit is contained in:
+30
-46
@@ -1,17 +1,12 @@
|
|||||||
import express from 'express';
|
import express from 'express';
|
||||||
import asyncHandler from 'express-async-handler';
|
import asyncHandler from 'express-async-handler';
|
||||||
import { model as HomebrewModel } from './homebrew.model.js';
|
import { model as HomebrewModel } from './homebrew.model.js';
|
||||||
|
|
||||||
const router = express.Router();
|
const router = express.Router();
|
||||||
|
|
||||||
const titleConditions = (title)=>{
|
const titleConditions = (title)=>{
|
||||||
if(!title) return {};
|
if(!title) return {};
|
||||||
return {
|
return { $text: { $search: title, $caseSensitive: false } };
|
||||||
$text : {
|
|
||||||
$search : title,
|
|
||||||
$caseSensitive : false,
|
|
||||||
},
|
|
||||||
};
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const authorConditions = (author)=>{
|
const authorConditions = (author)=>{
|
||||||
@@ -20,11 +15,9 @@ const authorConditions = (author)=>{
|
|||||||
};
|
};
|
||||||
|
|
||||||
const rendererConditions = (legacy, v3)=>{
|
const rendererConditions = (legacy, v3)=>{
|
||||||
if(legacy === 'true' && v3 !== 'true')
|
if(legacy === 'true' && v3 !== 'true') return { renderer: 'legacy' };
|
||||||
return { renderer: 'legacy' };
|
|
||||||
|
|
||||||
if(v3 === 'true' && legacy !== 'true')
|
if(v3 === 'true' && legacy !== 'true') return { renderer: 'V3' };
|
||||||
return { renderer: 'V3' };
|
|
||||||
|
|
||||||
return {}; // If all renderers selected, renderer field not needed in query for speed
|
return {}; // If all renderers selected, renderer field not needed in query for speed
|
||||||
};
|
};
|
||||||
@@ -33,36 +26,32 @@ const tagConditions = (tagsAnd, tagsOr, tagsNot)=>{
|
|||||||
const conditions = {};
|
const conditions = {};
|
||||||
|
|
||||||
if(tagsAnd.length > 0) {
|
if(tagsAnd.length > 0) {
|
||||||
conditions.tags = {
|
conditions.tags = { $all: tagsAnd.map((tag)=>tag.trim()) };
|
||||||
$all : tagsAnd.map((tag)=>tag.trim())
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tagsOr.length > 0) {
|
if(tagsOr.length > 0) {
|
||||||
conditions.tags = {
|
conditions.tags = { ...conditions.tags, $in: tagsOr.map((tag)=>tag.trim()) };
|
||||||
...conditions.tags,
|
|
||||||
$in : tagsOr.map((tag)=>tag.trim())
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(tagsNot.length > 0) {
|
if(tagsNot.length > 0) {
|
||||||
conditions.tags = {
|
conditions.tags = { ...conditions.tags, $nin: tagsNot.map((tag)=>tag.trim()) };
|
||||||
...conditions.tags,
|
|
||||||
$nin : tagsNot.map((tag)=>tag.trim())
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return conditions;
|
return conditions;
|
||||||
};
|
};
|
||||||
|
|
||||||
const sortConditions = (sort, dir)=>{
|
const sortConditions = (sort, dir)=>{
|
||||||
return {
|
return { [sort]: dir === 'asc' ? 1 : -1 };
|
||||||
[sort] : dir === 'asc' ? 1 : -1
|
};
|
||||||
};
|
|
||||||
|
const selectIndex = (req)=>{
|
||||||
|
const hasTags = !!(req.query.tagsAnd || req.query.tagsOr || req.query.tagsNot);
|
||||||
|
|
||||||
|
return hasTags && req.query.title.length === 0 ? 'published_1_tags_1' : null;
|
||||||
};
|
};
|
||||||
|
|
||||||
const buildVaultQuery = (req)=>{
|
const buildVaultQuery = (req)=>{
|
||||||
const title = req.query.title || '';
|
const title = req.query.title || '';
|
||||||
const author = req.query.author || '';
|
const author = req.query.author || '';
|
||||||
const tagsAnd = req.query.tagsAnd?.split(',') || [];
|
const tagsAnd = req.query.tagsAnd?.split(',') || [];
|
||||||
const tagsOr = req.query.tagsOr?.split(',') || [];
|
const tagsOr = req.query.tagsOr?.split(',') || [];
|
||||||
@@ -75,47 +64,42 @@ const buildVaultQuery = (req)=>{
|
|||||||
rendererConditions(req.query.legacy, req.query.v3),
|
rendererConditions(req.query.legacy, req.query.v3),
|
||||||
titleConditions(title),
|
titleConditions(title),
|
||||||
authorConditions(author),
|
authorConditions(author),
|
||||||
tagConditions(tagsAnd, tagsOr, tagsNot)
|
tagConditions(tagsAnd, tagsOr, tagsNot),
|
||||||
].filter((condition)=>Object.keys(condition).length > 0);
|
].filter((condition)=>Object.keys(condition).length > 0);
|
||||||
|
|
||||||
return { $and: conditions };
|
return { $and: conditions };
|
||||||
};
|
};
|
||||||
|
|
||||||
const findBrews = async (req, res)=>{
|
const findBrews = async (req, res)=>{
|
||||||
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
const page = Math.max(parseInt(req.query.page) || 1, 1);
|
||||||
const count = Math.max(parseInt(req.query.count) || 20, 10);
|
const count = Math.max(parseInt(req.query.count) || 20, 10);
|
||||||
const skip = (page - 1) * count;
|
const skip = (page - 1) * count;
|
||||||
const sort = req.query.sort || 'title';
|
const sort = req.query.sort || 'title';
|
||||||
const dir = req.query.dir || 'asc';
|
const dir = req.query.dir || 'asc';
|
||||||
|
|
||||||
const combinedQuery = buildVaultQuery(req);
|
const combinedQuery = buildVaultQuery(req);
|
||||||
|
|
||||||
const projection = {
|
const projection = { editId: 0, googleId: 0, text: 0, textBin: 0, version: 0 };
|
||||||
editId : 0,
|
|
||||||
googleId : 0,
|
|
||||||
text : 0,
|
|
||||||
textBin : 0,
|
|
||||||
version : 0
|
|
||||||
};
|
|
||||||
|
|
||||||
await HomebrewModel.find(combinedQuery, projection)
|
let query = HomebrewModel.find(combinedQuery, projection).sort(sortConditions(sort, dir)).skip(skip).limit(count).maxTimeMS(5000);
|
||||||
.sort(sortConditions(sort, dir))
|
|
||||||
.skip(skip)
|
const indexHint = selectIndex(req);
|
||||||
.limit(count)
|
if(indexHint) query = query.hint(indexHint);
|
||||||
.maxTimeMS(5000)
|
|
||||||
|
await query
|
||||||
.exec()
|
.exec()
|
||||||
.then((brews)=>{
|
.then((brews)=>{
|
||||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||||
|
|
||||||
const processedBrews = brews.map((brew)=>{
|
const processedBrews = brews.map((brew)=>{
|
||||||
brew.authors = brew.authors.map((author)=>emailRegex.test(author) ? 'hidden' : author
|
brew.authors = brew.authors.map((author)=>(emailRegex.test(author) ? 'hidden' : author));
|
||||||
);
|
|
||||||
return brew;
|
return brew;
|
||||||
});
|
});
|
||||||
|
|
||||||
res.json({ brews: processedBrews, page });
|
res.json({ brews: processedBrews, page });
|
||||||
})
|
})
|
||||||
.catch((error)=>{
|
.catch((error)=>{
|
||||||
throw { ...error, message: error.errorResponse?.errmsg || 'Error finding brews in Vault search', HBErrorCode: 90 };
|
throw { ...error, message: error.errorResponse?.errmsg || 'Error finding brews in Vault search', HBErrorCode: 90 };
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user