Skip to content

Commit

Permalink
Merge pull request #531 from julianpoy/recipe-reindex
Browse files Browse the repository at this point in the history
Recipe reindex
  • Loading branch information
julianpoy authored Aug 7, 2020
2 parents 86a4c72 + 57415d4 commit 8aaf156
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
19 changes: 17 additions & 2 deletions Backend/src/routes/recipes.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,11 +653,13 @@ router.delete(
},
transaction: t
})
})
}).then(() => {
return ElasticService.deleteRecipesByUser(res.locals.session.userId);
});
}).then(() => {
res.status(200).send({})
}).catch(next);
})
});

router.post(
'/delete-bulk',
Expand Down Expand Up @@ -775,5 +777,18 @@ router.delete(
.catch(next);
});

router.post('/reindex', MiddlewareService.validateSession(['user']), async (req, res, next) => {
const recipes = await Recipe.findAll({
where: {
userId: res.locals.session.userId,
}
});

await ElasticService.deleteRecipesByUser(res.locals.session.userId);

await ElasticService.indexRecipes(recipes);

res.status(200).send({});
});

module.exports = router;
22 changes: 22 additions & 0 deletions Backend/src/services/elastic.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,27 @@ const deleteRecipes = recipeIds => {
});
};

const deleteRecipesByUser = userId => {
if (!ENABLE) return Promise.resolve();

return client.deleteByQuery({
index: getFullIndexName('recipes'),
body: {
query: {
bool: {
filter: {
query_string: {
fields: ["userId"],
analyzer: "standard",
query: userId,
},
},
},
},
},
});
};

const searchRecipes = (userId, queryString) => {
if (!ENABLE) throw new Error("ElasticSearch not enabled");

Expand Down Expand Up @@ -142,5 +163,6 @@ const searchRecipes = (userId, queryString) => {
module.exports = {
indexRecipes,
deleteRecipes,
deleteRecipesByUser,
searchRecipes
};
6 changes: 3 additions & 3 deletions kube/configs/index-recipes.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
apiVersion: batch/v1
kind: Job
metadata:
name: rs-index-recipes-2020-03-10
name: rs-index-recipes-2020-08-07
spec:
template:
spec:
containers:
- name: rs-index-recipes-2020-03-10
- name: rs-index-recipes-2020-08-07
image: julianpoy/recipesage:api-2.4.0-b6
command: ["node"]
args: ["src/index.app.js"]
Expand All @@ -21,7 +21,7 @@ spec:
name: rs-api-env
env:
- name: INDEX_BEFORE
value: "2020-03-10 22:30"
value: "2020-08-07 00:00"
restartPolicy: Never
imagePullSecrets:
- name: myregistrykey
Expand Down

0 comments on commit 8aaf156

Please sign in to comment.