Mongo DB中的分面过滤器,适用于具有多种选项的产品

时间:2017-02-09 15:50:40

标签: mongodb aggregation-framework faceted-search

我们正在尝试创建对MangoDB的调用,以接收所有可能的产品过滤器。

我将尝试创建我们产品的示例

首款产品是Adidas Shoes,有两种颜色和尺寸可供选择。但对于不同的颜色,你有不同的尺寸。

{
    id: 1
    name: "Adidas Shoes",
        filters: [
        [
            {
                code: "brand",
                value: "Adidas"
            },
            {
                code: "colour",
                value: "white"
            },
            {
                code: "size",
                value: 41
            }
        ],
        [
            {
                code: "brand",
                value: "Adidas"
            },
            {
                code: "colour",
                value: "white"
            },
            {
                code: "size",
                value: 42
            }
        ],
        [
            {
                code: "brand",
                value: "Adidas"
            },
            {
                code: "colour",
                value: "white"
            },
            {
                code: "size",
                value: 43
            }
        ],
        [
            {
                code: "brand",
                value: "Adidas"
            },
            {
                code: "colour",
                value: "blue"
            },
            {
                code: "size",
                value: 41
            }
        ],
        [
            {
                code: "brand",
                value: "Adidas"
            },
            {
                code: "colour",
                value: "blue"
            },
            {
                code: "size",
                value: 44
            }
        ]
    ]
}

第二个产品是Nike Shoes。

{
    id: 2
    name: "Nike Shoes",
        filters: [
    [
        {
            code: "brand",
            value: "Nike",
        },
        {
            code: "colour",
            value: "white",
        },
        {
            code: "size",
            value: 41,
        }
    ],
    [
        {
            code: "brand",
            value: "Nike",
        },
        {
            code: "colour",
            value: "white",
        },
        {
            code: "size",
            value: 42,
        }
    ],
    [
        {
            code: "brand",
            value: "Nike",
        },
        {
            code: "colour",
            value: "green",
        },
        {
            code: "size",
            value: 41,
        }
    ]
]
}

和Reebook鞋

{
    id: 3
    name: "Reebook Shoes",
    filters: [
    [
        {
            code: "brand",
            value: "Reebook",
        },
        {
            code: "colour",
            value: "black",
        },
        {
            code: "size",
            value: 41,
        }
    ]
    ]
}

如您所见,选项大小取决于颜色,颜色取决于大小。

我们如何创建MongoDb.aggregate以获得所有可能的过滤器?

品牌:阿迪达斯(1),耐克(1),Reebook(1)

大小: 41(3),42(2),43(1),44(1)

颜色:白色(2),蓝色(1),绿色(1),黑色(1)

呼叫应该独立于我们拥有多少和哪些过滤器(具有一个选项的产品,具有更多选项和不同过滤器的产品)。你能解释一下如何在这种情况下使用$ group,$ unwind吗?以及我们如何用$ facet改进它?

非常感谢!!!

编辑02/10/2017

示例回复

facets: [
    {
        code: "brand",
        values: [
            {
                name: "Adidas",
                count: 1
            },
            {
                name: "Nike",
                count: 1
            },
            {   
                name: "Reebook",
                count: 1
            }
        ]
    },
    {
        code: "size",
        values: [
            {
                name: 41,
                count: 3
            },
            {
                name: 42,
                count: 2
            },
            {   
                name: 43,
                count: 1
            },
            {   
                name: 44,
                count: 1
            }
        ]
    },
    {
        code: "colour",
        values: [
            {
                name: "White",
                count: 2
            },
            {
                name: "Blue",
                count: 1
            },
            {   
                name: "Green",
                count: 1
            },
            {   
                name: "Black",
                count: 1
            }
        ]
    }
]

facets: {
    "brand": {
        "Adidas": 1,
        "Nike":1,
        "Reebook":1
    },
    "size": {
        "41": 3,
        "42":2,
        "43":1,
        "44":1
    },
    "colour": {
        "White": 2,
        "Blue":1,
        "Green":1,
        "Black":1
    }
}

这是我们的第一阶段。下一步将是当我选择尺寸:41和颜色:白色时搜索可能的滤镜。

由于

1 个答案:

答案 0 :(得分:2)

这是一个可能适合您的聚合。

db.getCollection('test').aggregate([
	{$unwind: '$filters'},
	{$unwind: '$filters'},
	{
		$group: {
			_id: {code: '$filters.code', value: '$filters.value'},
			products: {$addToSet: '$_id'}
		}
	},
	{
		$project: {
			'filter.value': '$_id.value',
			'filter.count': {$size: '$products'}
		}
	},
	{
		$group: {
			_id: '$_id.code',
			filters: {$push: '$filter'}
		}
	}
]);

您需要的数据来自slightly different format,因为没有简单的方法可以将分组值数组转换为对象属性。

如果已选择某些过滤器,则在第一次$展开后需要另一个$ match阶段。 它还支持多选。说我想要Reebook / Adidas制作的白色/黑色鞋子。

db.getCollection('test').aggregate([
	{$unwind: '$filters'},
	{
		$match: {
			$and: [
				//Add objects here fo everything that is selected already
				{'filters': {$elemMatch: {code: 'colour', value: {$in: ['black', 'white']}}}},
				{'filters': {$elemMatch: {code: 'brand', value: {$in: ['Adidas', 'Reebook']}}}}
			]
		}
	},
	{$unwind: '$filters'},
	{
		$group: {
			_id: {code: '$filters.code', value: '$filters.value'},
			products: {$addToSet: '$_id'}
		}
	},
	{
		$project: {
			'filter.value': '$_id.value',
			'filter.count': {$size: '$products'}
		}
	},
	{
		$group: {
			_id: '$_id.code',
			filters: {$push: '$filter'}
		}
	}
]);

最后一件事是这样的依赖行为: 选择Nike =>尺寸和颜色按品牌过滤,但您仍然可以选择所有品牌。 选择Nike + 42尺寸=>您只能选择尺寸为42,颜色和品牌的品牌,其中有42种尺码的鞋子。 等等。

您可以利用$ facet。事实上,当下一个想法。 如果我们要计算品牌 - 我们应该根据尺寸和颜色下拉选择过滤记录。 如果我们计算尺寸 - 应用颜色和品牌。颜色的逻辑相同。

以下是mongo shell中的代码:

//This hash is going to by dynamic
//Add and remove properties, change $in arrays
//Depending on what user does
var conditionsForUserSelect = {
	'colour': {'filters': {$elemMatch: {code: 'colour', value: {$in: ['green']}}}},
	'brand': {'filters': {$elemMatch: {code: 'brand', value: {$in: ['Nike']}}}},
	'size': {'filters': {$elemMatch: {code: 'size', value: {$in: [41]}}}}
};

var getFacetStage = function (code) {
	//Empty object, accept all filters if nothing is selected
	var matchStageCondition = {};

	var selectedFilters = Object.keys(conditionsForUserSelect);
	if (selectedFilters && selectedFilters.length) {
		//Take all queries EXCEPT for the passed
		//E.g. if we are counting brand filters then we should apply color and size.
		//Because for example if no size/colour selected we should
		//allow all brands even if Reebok is selected
		var conditionsToApply = selectedFilters
			.filter(function (key) {
				return key !== code
			})
			.map(function (key) {
				return conditionsForUserSelect[key]
			});

		if (conditionsToApply && conditionsToApply.length) {
			matchStageCondition = {
				$and: conditionsToApply
			};
		}
	}

	return [
		{$unwind: '$filters'},
		{
			$match: matchStageCondition
		},
		{$unwind: '$filters'},
		{
			$group: {
				_id: {code: '$filters.code', value: '$filters.value'},
				products: {$addToSet: '$_id'}
			}
		},
		{
			$project: {
				'filter.value': '$_id.value',
				'filter.count': {$size: '$products'}
			}
		},
		{
			$group: {
				_id: '$_id.code',
				filters: {$push: '$filter'}
			}
		},
		{
			$match: {_id: code}
		}
	];
};


db.getCollection('test').aggregate([
	{
		$facet: {
			colour: getFacetStage('colour'),
			size: getFacetStage('size'),
			brand: getFacetStage('brand')
		}
	}
]);