mirror of
https://github.com/naturalcrit/homebrewery.git
synced 2026-11-22 12:17:02 +00:00
only use tag index if only tags are present and not text
This commit is contained in:
1 parent
2eead59e78
commit
c9d0e05de8
1 file changed
+23
-39
+23
-39
@@ -6,12 +6,7 @@ const router = express.Router();
|
||||
|
||||
const titleConditions = (title)=>{
|
||||
if(!title) return {};
|
||||
return {
|
||||
$text : {
|
||||
$search : title,
|
||||
$caseSensitive : false,
|
||||
},
|
||||
};
|
||||
return { $text: { $search: title, $caseSensitive: false } };
|
||||
};
|
||||
|
||||
const authorConditions = (author)=>{
|
||||
@@ -20,11 +15,9 @@ const authorConditions = (author)=>{
|
||||
};
|
||||
|
||||
const rendererConditions = (legacy, v3)=>{
|
||||
if(legacy === 'true' && v3 !== 'true')
|
||||
return { renderer: 'legacy' };
|
||||
if(legacy === 'true' && v3 !== 'true') return { renderer: 'legacy' };
|
||||
|
||||
if(v3 === 'true' && legacy !== 'true')
|
||||
return { renderer: 'V3' };
|
||||
if(v3 === 'true' && legacy !== 'true') return { renderer: 'V3' };
|
||||
|
||||
return {}; // If all renderers selected, renderer field not needed in query for speed
|
||||
};
|
||||
@@ -33,32 +26,28 @@ const tagConditions = (tagsAnd, tagsOr, tagsNot)=>{
|
||||
const conditions = {};
|
||||
|
||||
if(tagsAnd.length > 0) {
|
||||
conditions.tags = {
|
||||
$all : tagsAnd.map((tag)=>tag.trim())
|
||||
};
|
||||
conditions.tags = { $all: tagsAnd.map((tag)=>tag.trim()) };
|
||||
}
|
||||
|
||||
if(tagsOr.length > 0) {
|
||||
conditions.tags = {
|
||||
...conditions.tags,
|
||||
$in : tagsOr.map((tag)=>tag.trim())
|
||||
};
|
||||
conditions.tags = { ...conditions.tags, $in: tagsOr.map((tag)=>tag.trim()) };
|
||||
}
|
||||
|
||||
if(tagsNot.length > 0) {
|
||||
conditions.tags = {
|
||||
...conditions.tags,
|
||||
$nin : tagsNot.map((tag)=>tag.trim())
|
||||
};
|
||||
conditions.tags = { ...conditions.tags, $nin: tagsNot.map((tag)=>tag.trim()) };
|
||||
}
|
||||
|
||||
return conditions;
|
||||
};
|
||||
|
||||
const sortConditions = (sort, dir)=>{
|
||||
return {
|
||||
[sort] : dir === 'asc' ? 1 : -1
|
||||
};
|
||||
return { [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)=>{
|
||||
@@ -75,7 +64,7 @@ const buildVaultQuery = (req)=>{
|
||||
rendererConditions(req.query.legacy, req.query.v3),
|
||||
titleConditions(title),
|
||||
authorConditions(author),
|
||||
tagConditions(tagsAnd, tagsOr, tagsNot)
|
||||
tagConditions(tagsAnd, tagsOr, tagsNot),
|
||||
].filter((condition)=>Object.keys(condition).length > 0);
|
||||
|
||||
return { $and: conditions };
|
||||
@@ -90,28 +79,23 @@ const findBrews = async (req, res)=>{
|
||||
|
||||
const combinedQuery = buildVaultQuery(req);
|
||||
|
||||
const projection = {
|
||||
editId : 0,
|
||||
googleId : 0,
|
||||
text : 0,
|
||||
textBin : 0,
|
||||
version : 0
|
||||
};
|
||||
const projection = { editId: 0, googleId: 0, text: 0, textBin: 0, version: 0 };
|
||||
|
||||
await HomebrewModel.find(combinedQuery, projection)
|
||||
.sort(sortConditions(sort, dir))
|
||||
.skip(skip)
|
||||
.limit(count)
|
||||
.maxTimeMS(5000)
|
||||
let query = HomebrewModel.find(combinedQuery, projection).sort(sortConditions(sort, dir)).skip(skip).limit(count).maxTimeMS(5000);
|
||||
|
||||
const indexHint = selectIndex(req);
|
||||
if(indexHint) query = query.hint(indexHint);
|
||||
|
||||
await query
|
||||
.exec()
|
||||
.then((brews)=>{
|
||||
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
||||
|
||||
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;
|
||||
});
|
||||
|
||||
res.json({ brews: processedBrews, page });
|
||||
})
|
||||
.catch((error)=>{
|
||||
|
||||
Reference in new issue
Block a user