电子商务中的 ElasticSearch:一个产品的多个类别

ElasticSearch in e-commerce: multiple categories for a product

您如何在 ElasticSearch 中为此类产品编制索引?我们根据属性(颜色、品牌、大小、用户输入的任何内容)将所有文档分开,但它们都属于一组类别。可能是一个,可能是15个。

        [0] => Array
            (
                [product_id] => 123456
                [product_name] => Shirt 1
                [filter_name] => Colour
                [filter_value] => Blue
                [product_parent_id] => 111111
                [product_has_discount] => 0
                [product_price] => 19.99
                [product_stock] => 1
            )
        [1] => Array
            (
                [product_id] => 123457
                [product_name] => Shirt 1
                [filter_name] => Colour
                [filter_value] => Red
                [product_parent_id] => 111111
                [product_has_discount] => 0
                [product_price] => 19.99
                [product_stock] => 1
            )

我们如何将类别标记到其中?会不会这么简单

        [product_categories] => ;4750;4834;4835;4836;

然后用 match against category 和值 ;4836; 查询 ElasticSearch?那可能吗?推荐?

您可以在映射中将 product_categories 定义为整数,并将类别值作为数组传递,如

 [product_categories] => array(4750,4834,4835,4836)

编辑:您阅读了有关映射的更多信息here. A more specific to mapping an array type

一旦您的数据像这样被编入索引,您就可以在字段上product_categories轻松地在所有组合中进行查询、过滤和聚合。

例如要匹配类别 4750 或 4750 中的产品:

{
    "filter": {
        "terms": {
            "product_categories": [
                4750,
                4750
            ]
        }
    }
}