Skip to content

Commit

Permalink
Merge pull request #51 from Chia-Network/feat/fts
Browse files Browse the repository at this point in the history
feat: fts queries
  • Loading branch information
MichaelTaylor3D authored Dec 15, 2021
2 parents f3f81e9 + fe0b754 commit 5b1fcba
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 6 deletions.
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

0 comments on commit 5b1fcba

Please sign in to comment.