From f5a803d86f437e508fcec382d59f54a2adb92831 Mon Sep 17 00:00:00 2001 From: morteza-mortezai Date: Tue, 7 Jul 2026 21:38:02 +0330 Subject: [PATCH] index products --- src/modules/product/models/product.model.ts | 8 ++++++++ src/modules/product/models/productVariant.model.ts | 4 ++++ src/modules/product/providers/product.service.ts | 1 + 3 files changed, 13 insertions(+) diff --git a/src/modules/product/models/product.model.ts b/src/modules/product/models/product.model.ts index 351499e..9396ef4 100644 --- a/src/modules/product/models/product.model.ts +++ b/src/modules/product/models/product.model.ts @@ -53,6 +53,14 @@ const productSchema = new Schema( { timestamps: true, toJSON: { virtuals: true, versionKey: false }, _id: false, id: false }, ); +productSchema.index({ status: 1, createdAt: -1 }, { partialFilterExpression: { deleted: false } }); +productSchema.index({ shop: 1, status: 1, createdAt: -1 }, { partialFilterExpression: { deleted: false } }); +productSchema.index({ category: 1, status: 1, createdAt: -1 }, { partialFilterExpression: { deleted: false } }); +productSchema.index({ brand: 1, status: 1, createdAt: -1 }, { partialFilterExpression: { deleted: false } }); +productSchema.index({ category: 1, brand: 1, status: 1 }, { partialFilterExpression: { deleted: false } }); +productSchema.index({ model: 1 }, { partialFilterExpression: { deleted: false } }); +productSchema.index({ tags: 1, status: 1 }, { partialFilterExpression: { deleted: false } }); + productSchema.virtual("url").get(function () { return `/product/SHP-${this._id}/${this.title_fa}`; }); diff --git a/src/modules/product/models/productVariant.model.ts b/src/modules/product/models/productVariant.model.ts index 0795fe6..b01c7d3 100644 --- a/src/modules/product/models/productVariant.model.ts +++ b/src/modules/product/models/productVariant.model.ts @@ -65,6 +65,10 @@ const productVariantSchema = new Schema( { timestamps: true, toObject: { virtuals: true, versionKey: false }, toJSON: { virtuals: true, versionKey: false }, id: false }, ); +productVariantSchema.index({ product: 1, deleted: 1 }); +productVariantSchema.index({ shop: 1, product: 1, deleted: 1 }); +productVariantSchema.index({ market_status: 1, deleted: 1 }); + productVariantSchema.pre("save", function (next) { if (this.price) { if (this.price.retailPrice) { diff --git a/src/modules/product/providers/product.service.ts b/src/modules/product/providers/product.service.ts index a70f3d3..eef1239 100644 --- a/src/modules/product/providers/product.service.ts +++ b/src/modules/product/providers/product.service.ts @@ -106,6 +106,7 @@ class ProductService { { title_fa: { $regex: q, $options: "i" } }, { model: { $regex: q, $options: "i" } }, ], + deleted: false, variants: { $elemMatch: { $exists: true } }, }, { _id: 1, title_fa: 1, title_en: 1, imagesUrl: 1, model: 1, description: 1, variants: 1 },