首页 > 解决方案 > 使用 $facet 和 mongoDB $search 进行过滤器导航

问题描述

与过滤器一起使用时,如何制作不随 $search 改变的品牌。

    const products = await Product.aggregate([
  {
    $search: {
      compound: {
        must: finalQuery,
      },
    },
  },

  { $skip: pageSize * (page - 1) },
  { $limit: pageSize },

  {
    $project: {
      _id: 1,
      name: 1,
      brand: 1,
      productCollection: 1,
      department: 1,
      category: 1,
      subcategory: 1,
      color: 1,
      price: 1,
      images: 1,
      size_names: 1,
      sizes: 1,
      productType: 1,
      productTag: 1,
      counter: 1,
      discountDisplayLabel: 1,
      discount: 1,
      mrp: 1,
      createdAt: 1,
      tags: 1,
      slug: 1,
      // score: { $meta: "searchScore" },
    },
  },
]);


const filters = await Product.aggregate([
  {
    $search: {
      compound: {
        must: finalQuery,
      },
    },
  },
  {
    $facet: {
      category_filters: [
        {
          $group: {
            _id: "$category",
            count: { $sum: 1 },
          },
        },
        { $sort: { count: -1, _id: -1 } },
      ],
      subcategory_filters: [
        {
          $group: {
            _id: "$subcategory",
            count: { $sum: 1 },
          },
        },
        { $sort: { count: -1, _id: -1 } },
      ],
      brands_filters: [
        {
          $group: {
            _id: "$brandSlug",
            count: { $sum: 1 },
          },
        },
        { $sort: { count: -1, _id: -1 } },
      ],

      size_names_filters: [
        { $limit: 10000 },
        { $unwind: "$sizes" },
        {
          $group: {
            _id: "$sizes.name",
            count: { $sum: 1 },
          },
        },
        { $sort: { count: -1, _id: -1 } },
      ],

      prices_filters: [
        {
          $bucketAuto: {
            groupBy: "$price",
            buckets: 5,
          },
        },
      ],
    },
  },
]);

这个的输出:

{ products:[
{
            "_id": "607170bb46d0eb352657b9a1",
            "productCollection": [
                "korean-cosmetics"
            ],
            "productTag": "NORMAL",
            "mrp": 38,
            "price": 38,
            "discount": 0,
            "images": [
                {
                    "public_id": "image key",
                    "url": "image url"
                },
        
            ],
            "size_names": "One Size",
            "counter": 2,
            "name": "Product name",
            "brand": "brand name",
            "category": "health-and-beauty",
            "subcategory": "makeup",
            "department": "UNISEX",
            "productType": "LIFESTYLE",
            "color": "",
            "sizes": [
                {
                    "name": "One Size",
                    "sku": "ABC12",
                    "quantity": 2
                }
            ],
            "createdAt": "2021-04-10T09:32:43.194Z",
            "slug": "propolis-light-cream-65gm",
            "tags": [
        
                "gentle-care",
                "unisex",
                "designer",
                "fashion",
                "best",
                "100%",
                "under",
                "affordable",
                "beautiful",
                "healthy",
                "fashion",
                "makeup",
                "beauty"
            ]
        },
],......,
    "filters": [
        {
            "category_filters": [
                {
                    "_id": "health-and-beauty",
                    "count": 51
                }
            ],
            "subcategory_filters": [
                {
                    "_id": "k-beauty",
                    "count": 51
                }
            ],
            "brands_filters": [
                {
                    "_id": "cosrx",
                    "count": 26
                },
                {
                    "_id": "innisfree",
                    "count": 8
                },
                {
                    "_id": "laneige",
                    "count": 5
                },
                {
                    "_id": "it-s-skin",
                    "count": 4
                },
                {
                    "_id": "the-face-shop",
                    "count": 3
                },
                {
                    "_id": "dr-jart",
                    "count": 3
                },
                {
                    "_id": "tonymoly",
                    "count": 2
                }
            ],
            "size_names_filters": [
                {
                    "_id": "One Size",
                    "count": 49
                },
                {
                    "_id": "onesize",
                    "count": 2
                }
            ],
            "prices_filters": [
                {
                    "_id": {
                        "min": 5,
                        "max": 17
                    },
                    "count": 10
                },
                {
                    "_id": {
                        "min": 17,
                        "max": 25
                    },
                    "count": 12
                },
                {
                    "_id": {
                        "min": 25,
                        "max": 29
                    },
                    "count": 13
                },
                {
                    "_id": {
                        "min": 29,
                        "max": 39
                    },
                    "count": 10
                },
                {
                    "_id": {
                        "min": 39,
                        "max": 89
                    },
                    "count": 6
                }
            ]
        }
    ]

}

现在,如果我单击品牌,它也不应该隐藏其他品牌。

示例 - 我得到的产品与品牌 X、品牌 Y 和品牌 Z 相关

当我添加brandX过滤器时..

brandY 和brandZ 也被过滤掉了。。

我想显示brandX的结果及其适当的尺寸以及在UI中仍然可见的brandY和brandZ

标签: mongodbsearchaggregation-frameworkaggregatefaceted-search

解决方案


推荐阅读