Elasticsearch商品搜索

本文介绍了使用Elasticsearch进行商品搜索的方法,包括简化后商品的数据结构设计和查询语句的构建。用户输入的搜索词可以匹配商品名称、SKU或价格。例如,搜索“电脑”会检查是否为数字以进行价格匹配,而输入“6000”则能直接转化为数字进行有效搜索。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

数据结构

简化后的商品结构

{
                    "name": "测试鼠标1",    //  商品名称
                    "productId": 2,              // 商品ID
                    "status": 2,
                    "add_time": "2015-11-12 00:00:00",
                    "last_update_time": "2020-06-12 00:00:00",
                    "sku": [
                        "white",
                        "yellow"
                    ],
                    "skuPrice": [
                        14,
                        22
                    ]
 }

查询语句

用户输入的可以搜索到匹配商品名称、商品sku、商品价格。比如用户输入“电脑”,判断“电脑”是否为数字,如果不是价格匹配值为-1

{
    "query": {
        "bool": {
            "must": {
                "bool" : { 
                    "should": [
                        { "match": { "name": "电脑" }},
                        { "terms": { "sku": ["电脑"] }},
                        { "terms": { "skuPrice": [ -1 ] }}
                    ]
                }
            }
        }
    }
}

结果

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 0.9400072,
        "hits": [
            {
                "_index": "products1",
                "_type": "_doc",
                "_id": "226AN3MBpaBcVbqwZMC2",
                "_score": 0.9400072,
                "_source": {
                    "name": "测试电脑1",
                    "productId": 2,
                    "status": 2,
                    "add_time": "2015-11-12 00:00:00",
                    "last_update_time": "2020-06-12 00:00:00",
                    "sku": [
                        "red",
                        "blue"
                    ],
                    "skuPrice": [
                        5500,
                        4400
                    ]
                }
            },
            {
                "_index": "products1",
                "_type": "_doc",
                "_id": "3G6AN3MBpaBcVbqw2MA4",
                "_score": 0.9400072,
                "_source": {
                    "name": "测试电脑2",
                    "productId": 2,
                    "status": 2,
                    "add_time": "2015-11-12 00:00:00",
                    "last_update_time": "2020-06-12 00:00:00",
                    "sku": [
                        "white",
                        "gray"
                    ],
                    "skuPrice": [
                        6600,
                        7700
                    ]
                }
            }
        ]
    }
}

用户输入 6000,可以转为数字。

{
    "query": {
        "bool": {
            "must": {
                "bool" : { 
                    "should": [
                        { "match": { "name": "6000" }},
                        { "terms": { "sku": ["6000"] }},
                        { "terms": { "skuPrice": [6000 ] }}
                    ]
                }
            }
        }
    }
}

结果

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "products1",
                "_type": "_doc",
                "_id": "3G6AN3MBpaBcVbqw2MA4",
                "_score": 1.0,
                "_source": {
                    "name": "测试电脑2",
                    "productId": 2,
                    "status": 2,
                    "add_time": "2015-11-12 00:00:00",
                    "last_update_time": "2020-06-12 00:00:00",
                    "sku": [
                        "white",
                        "gray"
                    ],
                    "skuPrice": [
                        6600,
                        7700
                    ]
                }
            }
        ]
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值