我想查找所有以“iphone 5”开头的建议,忽略案例。
我不明白为什么这个表达
/iphone 5.*/gi
无法与任何数据匹配。我已经核实了
regex101
它奏效了。
文档映射:
{
"properties": {
"_class": {
"type": "keyword",
"index": false,
"doc_values": false
},
"id": {
"type": "keyword"
},
"suggest": {
"type": "completion",
"analyzer": "simple",
"preserve_separators": true,
"preserve_position_increments": true,
"max_input_length": 50,
"contexts": [
{
"name": "context",
"type": "CATEGORY"
}
]
}
}
}
数据示例
{
"_index": "product_name_suggest",
"_id": "c349997fc3774baf902e2def411c8616ar",
"_score": 1.0,
"_source": {
"_class": "com.baoyihui.screw.model.elasticsearch.entity.ProductNameSuggest",
"id": "c349997fc3774baf902e2def411c8616ar",
"suggest": {
"input": [
"Samsung Galaxy Note Galaxy Note 3 Neo SM-N7505L",
"Galaxy Note Galaxy Note 3 Neo SM-N7505L",
"Note Galaxy Note 3 Neo SM-N7505L",
"Galaxy Note 3 Neo SM-N7505L",
"Note 3 Neo SM-N7505L",
"3 Neo SM-N7505L",
"Neo SM-N7505L",
"SM-N7505L"
],
"contexts": {
"context": [
"ar_machine_CompleteMachineDoc"
]
}
}
}
}
请求数据
http://127.0.0.1:9200/product_name_suggest/_searchï¼
{
"suggest": {
"suggest": {
"regex": "/iphone 5.*/gi",
"completion": {
"field": "suggest",
"size": 20,
"contexts": {
"context": [
{
"context": "zh_battery_SparePartsDoc"
}
]
},
"regex": {
"flags": "ALL"
},
"skip_duplicates": true
}
}
}
}
resposne:
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 0,
"relation": "eq"
},
"max_score": null,
"hits": []
},
"suggest": {
"suggest": [
{
"text": "/iphone 5.*/gi",
"offset": 0,
"length": 14,
"options": []
}
]
}
}
我确信有匹配的数据。因为我替换时接口返回的数据
"regex": "/iphone 5.*/gi"
具有
"prefix": "iphone 5"
.
{
"text": "iPhone 11 A2221 çµæ± ",
"_index": "product_name_suggest",
"_id": "095539e21a3d4bdea80d8dacda5e9a2czh",
"_score": 1.0,
"_source": {
"_class": "com.baoyihui.screw.model.elasticsearch.entity.ProductNameSuggest",
"id": "095539e21a3d4bdea80d8dacda5e9a2czh",
"suggest": {
"input": [
"è¹æ iPhone 11 A2221 çµæ± ",
"iPhone 11 A2221 çµæ± ",
"11 A2221 çµæ± ",
"A2221 çµæ± ",
"çµæ± "
],
"contexts": {
"context": [
"zh_battery_SparePartsDoc"
]
}
}
},
"contexts": {
"context": [
"zh_battery_SparePartsDoc"
]
}
}
但正如你所看到的,在用
“前缀”:“iphone5”
,它返回不相关的数据,这就是我想使用正则表达式的原因。