Skip to content

Commit

Permalink
Merge pull request #62 from Chia-Network/dev/michael.taylor/fts-fixes
Browse files Browse the repository at this point in the history
fix: fts fixes so that they are index correctly
  • Loading branch information
MichaelTaylor3D authored Dec 20, 2021
2 parents 32c6d37 + c801d3f commit 1a7b1a2
Show file tree
Hide file tree
Showing 13 changed files with 139 additions and 99 deletions.
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ To run a FTS query on a supported table, you can use the `MATCH` operator. A vir
Example:

```
SELECT * FROM projects WHERE projectName MATCH "partial name" ORDER BY rank
SELECT rank, * FROM projects_fts WHERE projects_fts MATCH "PartialMatch*" ORDER BY rank
The '*' in the match is needed for wildcard
```

More info: https://www.sqlite.org/fts5.html
More info: https://www.sqlite.org/fts5.html
4 changes: 0 additions & 4 deletions jsconfig.json

This file was deleted.

44 changes: 42 additions & 2 deletions migrations/20211212200953-fulltext-search.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,50 @@ module.exports = {
up: async (queryInterface, Sequelize) => {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE projects_fts USING fts5(projects);
CREATE VIRTUAL TABLE projects_fts USING fts5(
id,
warehouseProjectId,
projectId,
projectLocationId,
currentRegistry,
registryOfOrigin,
originProjectId,
program,
projectName,
projectLink,
projectDeveloper,
sector,
projectType,
coveredByNDC,
NDCLinkage,
projectStatus,
projectStatusDate,
unitMetric,
methodology,
methodologyVersion,
validationApproach,
validationDate,
projectTag,
estimatedAnnualAverageEmissionReduction
);
`);
await queryInterface.sequelize.query(`
CREATE VIRTUAL TABLE units_fts USING fts5(units);
CREATE VIRTUAL TABLE units_fts USING fts5(
id,
buyer,
registry,
blockIdentifier,
identifier,
unitType,
unitCount,
unitStatus,
unitStatusDate,
transactionType,
unitIssuanceLocation,
unitLink,
correspondingAdjustment,
unitTag
);
`);
}
},
Expand Down
11 changes: 6 additions & 5 deletions migrations/20211219182106-sqlite-triggers-projects.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -108,13 +108,14 @@ module.exports = {
);
END;
`);
}},
}
},

down: async (queryInterface, Sequelize) => {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.sequelize.query("DROP TRIGGER project_insert_fts;");
await queryInterface.sequelize.query("DROP TRIGGER project_delete_fts;");
await queryInterface.sequelize.query("DROP TRIGGER project_update_fts;");
await queryInterface.sequelize.query('DROP TRIGGER project_insert_fts;');
await queryInterface.sequelize.query('DROP TRIGGER project_delete_fts;');
await queryInterface.sequelize.query('DROP TRIGGER project_update_fts;');
}
}
},
};
18 changes: 6 additions & 12 deletions migrations/20211219184405-sqlite-triggers-units.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,10 @@ module.exports = {
CREATE TRIGGER unit_insert_fts AFTER INSERT ON units BEGIN
INSERT INTO units_fts(
id,
projectId,
buyer,
registry,
blockIdentifier,
identifier,
qualificationId,
unitType,
unitCount,
unitStatus,
Expand All @@ -21,16 +19,13 @@ module.exports = {
unitIssuanceLocation,
unitLink,
correspondingAdjustment,
unitTag,
vintageId
unitTag
) VALUES (
new.id,
new.projectId,
new.buyer,
new.registry,
new.blockIdentifier,
new.identifier,
new.qualificationId,
new.unitType,
new.unitCount,
new.unitStatus,
Expand All @@ -39,8 +34,7 @@ module.exports = {
new.unitIssuanceLocation,
new.unitLink,
new.correspondingAdjustment,
new.unitTag,
new.vintageId
new.unitTag
);
END;`);

Expand Down Expand Up @@ -97,9 +91,9 @@ module.exports = {

down: async (queryInterface, Sequelize) => {
if (queryInterface.sequelize.getDialect() === 'sqlite') {
await queryInterface.sequelize.query("DROP TRIGGER unit_insert_fts;");
await queryInterface.sequelize.query("DROP TRIGGER unit_delete_fts;");
await queryInterface.sequelize.query("DROP TRIGGER unit_update_fts;");
await queryInterface.sequelize.query('DROP TRIGGER unit_insert_fts;');
await queryInterface.sequelize.query('DROP TRIGGER unit_delete_fts;');
await queryInterface.sequelize.query('DROP TRIGGER unit_update_fts;');
}
}
},
};
9 changes: 6 additions & 3 deletions src/controllers/staging.controller.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import _ from 'lodash';
import * as fullNode from '../fullnode';

import { Staging, StagingMock, Project, Unit } from '../models';

export const findAll = async (req, res) => {
Expand Down Expand Up @@ -52,9 +53,11 @@ export const findAll = async (req, res) => {
};

export const commit = async (req, res) => {
const queryResponse = await Staging.findAll();
const stagingRecords = queryResponse.dataValues;
stagingRecords.forEach(async (stagingRecord) => {
const queryResponses = await Staging.findAll();

queryResponses.forEach(async (queryResponse) => {
const stagingRecord = queryResponse.dataValues;

const {
id: stagingRecordId,
uuid,
Expand Down
30 changes: 20 additions & 10 deletions src/fullnode/simulator.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,26 @@ const THIRTY_SEC = 30000;
// Simulate 30 seconds passing before commited to node

export const updateProjectRecord = async (uuid, record, stagingRecordId) => {
await deleteProjectRecord(uuid);
await createProjectRecord(uuid, record);

if (stagingRecordId) {
await Staging.destroy({
where: {
id: stagingRecordId,
},
});
}
return new Promise((resolve) => {
setTimeout(async () => {
if (stagingRecordId) {
await Project.destroy({
where: {
warehouseProjectId: uuid,
},
});
await Project.create({
...record,
warehouseProjectId: uuid,
});
await Staging.destroy({
where: {
id: stagingRecordId,
},
});
}
}, THIRTY_SEC);
});
};

export const createProjectRecord = (uuid, record, stagingRecordId) => {
Expand Down
22 changes: 2 additions & 20 deletions src/models/projects/projects.model.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,29 +39,11 @@ class Project extends Model {
}

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
ORDER BY rank DESC`;
const sql = `SELECT * FROM projects_fts WHERE projects_fts MATCH :search ORDER BY rank DESC`;

return sequelize.query(sql, {
model: Project,
replacements: { search: queryStr },
replacements: { search: `${queryStr}*` },
mapToModel: true, // pass true here if you have any mapped fields
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/models/projects/projects.modeltypes.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module.exports = {
autoIncrement: true,
},
warehouseProjectId: Sequelize.STRING,
projectID: Sequelize.STRING,
projectId: Sequelize.STRING,
projectLocationId: Sequelize.NUMBER,
currentRegistry: Sequelize.STRING,
registryOfOrigin: Sequelize.STRING,
Expand Down
Loading

0 comments on commit 1a7b1a2

Please sign in to comment.