Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: fts queries #51

Merged
merged 2 commits into from
Dec 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions migrations/20211212200953-fulltext-search.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module.exports = {
up: async (queryInterface, Sequelize) => {
if (queryInterface.sequelize.dialect == 'sqlite') {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.sequelize.query(`
create virtual table Projects using fts5
(warehouseProjectId, currentRegistry, registryOfOrigin,
Expand All @@ -20,7 +20,7 @@ module.exports = {
},

down: async (queryInterface, Sequelize) => {
if (queryInterface.sequelize.dialect == 'sqlite') {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.sequelize.query(`drop virtual table Projects;`);
await queryInterface.sequelize.query("create virtual table Units using fts5" +
"(ProjectId, owner, buyer, registry, blockIdentifier, identifier," +
Expand Down
18 changes: 14 additions & 4 deletions src/controllers/project.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,27 @@ export const findAll = async (req, res) => {
return;
}

res.json(
await Project.findAll({
const dialect = sequelize.getDialect();

if (req.query.search) {
if (dialect === 'sqlite') {
res.json(await Project.findAllSqliteFts(req.query.search));
} else if (dialect === 'mysql') {
res.json(await Project.findAllMySQLFts(req.query.search));
}

} else {
res.json(await Project.findAll({
include: [
ProjectLocation,
Qualification,
Vintage,
CoBenefit,
RelatedProject,
],
}),
);
}));
}

};

export const findOne = (req, res) => {
Expand Down
48 changes: 48 additions & 0 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,54 @@ class Project extends Model {
Project.hasMany(CoBenefit);
Project.hasMany(ProjectLocation);
}

static findAllMySQLFts(queryStr) {
const sql = `SELECT * FROM projects WHERE MATCH(
warehouseProjectId, currentRegistry, registryOfOrigin, program, projectName,
projectLink, projectDeveloper, sector, projectType, NDCLinkage, projectStatus,
unitMetric, methodology, methodologyVersion, validationApproach, projectTag,
estimatedAnnualAverageEmissionReduction, owner
) AGAINST ":search" ORDER BY relevance DESC`;

return sequelize
.query(sql, {
model: Project,
replacements: { search: queryStr },
mapToModel: true // pass true here if you have any mapped fields
});

}

static findAllSqliteFts(queryStr) {
const sql = `SELECT * FROM projects WHERE
warehouseProjectId MATCH ":search" OR
currentRegistry MATCH ":search" OR
registryOfOrigin MATCH ":search" OR
program MATCH ":search" OR
projectName MATCH ":search" OR
projectLink MATCH ":search" OR
projectDeveloper MATCH ":search" OR
sector MATCH ":search" OR
projectType MATCH ":search" OR
NDCLinkage MATCH ":search" OR
projectStatus MATCH ":search" OR
unitMetric MATCH ":search" OR
methodology MATCH ":search" OR
methodologyVersion MATCH ":search" OR
validationApproach MATCH ":search" OR
projectTag MATCH ":search" OR
estimatedAnnualAverageEmissionReduction MATCH ":search" OR
owner MATCH ":search"
ORDER BY rank DESC`;

return sequelize
.query(sql, {
model: Project,
replacements: { search: queryStr },
mapToModel: true // pass true here if you have any mapped fields
});

}
}

Project.init(
Expand Down