数据结构
简化后的商品结构
{
"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
]
}
}
]
}
}