php实现elasticsearch商品属性

备注:
代码都是使用 elasticsearch/elasticsearch扩展操作es。如果更好方案,欢迎交流

思路:
使用嵌套类型 nested 存储属性数据

php 代码

省略......................

设置mapping信息

$query = [
            'index' => 'goods',
            'body' => [
                'properties' => [
                    'goods_name' => [
                        'type' => 'text',
                        'analyzer' => 'ik_max_word',
                        'search_analyzer' => 'ik_smart'
                    ],
                    'goods_id' => [
                        'type' => 'long'
                    ],
                    'attr' => [
                        'type' => 'nested',
                        'properties' => [
                            'attr_name' => [
                                'type' => 'keyword'
                            ],
                            'attr_value' => [
                                'type' => 'keyword'
                            ]
                        ]
                    ]
                ]
            ] 
        ];
        
$this->client->indices()->putMapping($query);

新增商品信息,属性名称可以根据需求记录属性id

$query = [
            'index' => 'goods',
            'id'  => 1,
            'body' => [
                'goods_id' => 1,
                'goods_name' => '小米手机 小米10 68g',
                'attr' => [
                    [
                        'attr_name' => '操作系统',
                        'attr_valued' => '安卓'
                    ],
                    [
                        'attr_name' => '颜色',
                        'attr_valued' => '红色'
                    ],
                    [
                        'attr_name' => '屏幕尺寸',
                        'attr_valued' => '4.5'
                    ],
                    [
                        'attr_name' => '重量',
                        'attr_valued' => '100'
                    ]
                ]
            ]
        ];
$res = $this->client->index($query);

商品搜索,属性筛选

$query = [
            'index' => 'goods',
            'body' => [
                'query' => [
                    'bool' => [
                        'must' => [
                            [
                                'nested' => [
                                    'path' => 'attr',
                                    'query' => [
                                        'bool' => [
                                            'must' => [
                                                'match' => [
                                                    'attr.attr_name' => '操作系统'
                                                ],
                                                'match' => [
                                                    'attr.attr_value' => '安卓'
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ],
                            [
                                'nested' => [
                                    'path' => 'attr',
                                    'query' => [
                                        'bool' => [
                                            'must' => [
                                                'match' => [
                                                    'attr.attr_name' => '颜色'
                                                ],
                                                'match' => [
                                                    'attr.attr_value' => '红色'
                                                ]
                                            ]
                                        ]
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ];
$data = $this->client->search($query);

你可能感兴趣的:(php实现elasticsearch商品属性)