Add search text index is Asset
MODIFY
src/storage/mongodb/AssetStorage.ts
src/storage/mongodb/AssetStorage.ts
@@ -13,7 +13,6 @@ import Utils from '../../utils/Utils';
const MODULE_NAME = 'AssetStorage';
export default class AssetStorage {
-
public static async getAsset(tenantID: string, id: string = Constants.UNKNOWN_OBJECT_ID,
params: { withSiteArea?: boolean} = {}): Promise<Asset> {
// Debug
@@ -27,18 +26,14 @@ export default class AssetStorage {
},
Constants.DB_PARAMS_SINGLE_RECORD
);
- let asset: Asset = null;
+ let assetMDB: Asset = null;
// Check
if (assetsMDB && assetsMDB.count > 0) {
- asset = assetsMDB.result[0];
+ assetMDB = assetsMDB.result[0];
}
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'getAsset', uniqueTimerID,
- {
- id,
- withSiteArea: params.withSiteArea
- });
- return asset;
+ Logging.traceEnd(tenantID, MODULE_NAME, 'getAsset', uniqueTimerID, assetsMDB);
+ return assetMDB;
}
public static async getAssetImage(tenantID: string, id: string): Promise<{ id: string; image: string }> {
@@ -50,7 +45,7 @@ export default class AssetStorage {
const assetImageMDB = await global.database.getCollection<{ _id: ObjectID; image: string }>(tenantID, 'assetimages')
.findOne({ _id: Utils.convertToObjectID(id) });
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'getAssetImage', uniqueTimerID, { id });
+ Logging.traceEnd(tenantID, MODULE_NAME, 'getAssetImage', uniqueTimerID, assetImageMDB);
return {
id: id,
image: assetImageMDB ? assetImageMDB.image : null
@@ -107,7 +102,7 @@ export default class AssetStorage {
await AssetStorage.saveAssetImage(tenantID, assetMDB._id.toHexString(), assetToSave.image);
}
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'saveAsset', uniqueTimerID, { assetToSave });
+ Logging.traceEnd(tenantID, MODULE_NAME, 'saveAsset', uniqueTimerID, assetToSave);
return assetMDB._id.toHexString();
}
@@ -125,14 +120,13 @@ export default class AssetStorage {
dbParams.limit = Utils.checkRecordLimit(dbParams.limit);
// Check Skip
dbParams.skip = Utils.checkRecordSkip(dbParams.skip);
+ // Create Aggregation
+ const aggregation = [];
// Set the filters
const filters: FilterParams = {};
- // Build filter
+ // Search
if (params.search) {
- const searchRegex = Utils.escapeSpecialCharsInRegex(params.search);
- filters.$or = [
- { 'name': { $regex: searchRegex, $options: 'i' } },
- ];
+ filters.$text = { $search: params.search };
}
// With no Site Area
if (params.withNoSiteArea) {
@@ -146,8 +140,6 @@ export default class AssetStorage {
if (params.dynamicOnly) {
filters.dynamicAsset = true;
}
- // Create Aggregation
- const aggregation = [];
// Limit on Asset for Basic Users
if (!Utils.isEmptyArray(params.assetIDs)) {
// Build filter
@@ -173,6 +165,7 @@ export default class AssetStorage {
// Check if only the total count is requested
if (dbParams.onlyRecordCount) {
// Return only the count
+ Logging.traceEnd(tenantID, MODULE_NAME, 'getAssets', uniqueTimerID, assetsCountMDB);
return {
count: (assetsCountMDB.length > 0 ? assetsCountMDB[0].count : 0),
result: []
@@ -211,20 +204,18 @@ export default class AssetStorage {
// Project
DatabaseUtils.projectFields(aggregation, projectFields);
// Read DB
- const assets = await global.database.getCollection<any>(tenantID, 'assets')
+ const assetsMDB = await global.database.getCollection<any>(tenantID, 'assets')
.aggregate(aggregation, {
collation: { locale: Constants.DEFAULT_LOCALE, strength: 2 },
allowDiskUse: true
})
.toArray();
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'getAssets', uniqueTimerID,
- { params, limit: dbParams.limit, skip: dbParams.skip, sort: dbParams.sort });
- // Ok
+ Logging.traceEnd(tenantID, MODULE_NAME, 'getAssets', uniqueTimerID, assetsMDB);
return {
count: (assetsCountMDB.length > 0 ?
(assetsCountMDB[0].count === Constants.DB_RECORD_COUNT_CEIL ? -1 : assetsCountMDB[0].count) : 0),
- result: assets
+ result: assetsMDB
};
}
@@ -305,8 +296,7 @@ export default class AssetStorage {
})
.toArray();
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'getAssetsInError', uniqueTimerID,
- { params, limit: dbParams.limit, skip: dbParams.skip, sort: dbParams.sort });
+ Logging.traceEnd(tenantID, MODULE_NAME, 'getAssetsInError', uniqueTimerID, assetsMDB);
// Ok
return {
count: assetsMDB.length,
@@ -326,7 +316,7 @@ export default class AssetStorage {
await global.database.getCollection<any>(tenantID, 'assetimages')
.findOneAndDelete({ '_id': Utils.convertToObjectID(id) });
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'deleteAsset', uniqueTimerID, { id });
+ Logging.traceEnd(tenantID, MODULE_NAME, 'deleteAsset', uniqueTimerID, id);
}
private static async saveAssetImage(tenantID: string, assetID: string, assetImageToSave: string): Promise<void> {
@@ -340,7 +330,7 @@ export default class AssetStorage {
{ $set: { image: assetImageToSave } },
{ upsert: true });
// Debug
- Logging.traceEnd(tenantID, MODULE_NAME, 'saveAssetImage', uniqueTimerID, {});
+ Logging.traceEnd(tenantID, MODULE_NAME, 'saveAssetImage', uniqueTimerID, assetImageToSave);
}
private static getAssetInErrorFacet(errorType: string) {

MODIFY
src/storage/mongodb/MongoDBStorage.ts
src/storage/mongodb/MongoDBStorage.ts
@@ -68,6 +68,10 @@ export default class MongoDBStorage {
{ fields: { 'address.coordinates': '2dsphere' } },
]);
await this.handleIndexesInCollection(tenantID, 'eulas');
+ // Assets
+ await this.handleIndexesInCollection(tenantID, 'assets', [
+ { fields: { name: 'text' } },
+ ]);
// Logs
await this.handleIndexesInCollection(tenantID, 'logs', [
{ fields: { timestamp: 1 } },

Add search text index is Asset