Faceted filters in Mongo DB with for products with multiple options

女生的网名这么多〃 提交于 2019-12-22 16:38:42

问题


we are trying to create call to MangoDB to receive all possible filters for products.

I will try to create example of our products

First product is Adidas Shoes which have two options to select - colour and size. But for different colours you have different sizes.

{
    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
            }
        ]
    ]
}

Second product is 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,
        }
    ]
]
}

And Reebook shoes

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

as you can see option size is dependent on colour and colour is dependent on size.

How we can create MongoDb.aggregate to have all possible filters?

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)

And call should be independent on how many and which filters we have (products with one option, products with more options and different filters). Can you explain how to use $group, $unwind in this situation? And how we can improve it later with $facet?

Thanks a lot!!!

EDIT 02/10/2017

Sample response

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
            }
        ]
    }
]

or

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
    }
}

This is our first stage. Next step will be how to search possible filters when I have selected Size: 41 and Colour: White.

Thanks


回答1:


Here is an aggregation that might work for you.

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'}
		}
	}
]);

The data you need comes in a slightly different format because there's no easy way to convert array of grouped values to object properties.

If some filters are already selected you need another $match stage after the first $unwind. It also supports multi selects. Say I want white/black shoes made by 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'}
		}
	}
]);

The last thing is the dependent behavior like this: Select Nike => Size and Color are filtered by brand but you are still able to select all brands. Select Nike + 42 Size => You can select only brands having size 42, colors and brands for which there are shoes of 42 size. And so on.

You can leverage $facet for it. In fact when the idea is next. If we're calculating brands - we should filter records by what's selected in size and color dropdowns. If we're calculating size - applying color and brand. Same logic for color.

Here is the code that worked in 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')
		}
	}
]);


来源:https://stackoverflow.com/questions/42141116/faceted-filters-in-mongo-db-with-for-products-with-multiple-options

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!