我想查找所有以“iPhone 5”开头的建议,忽略大小写。我不明白为什么这个表达式/iphone 5.*/gi
不能匹配任何数据。我已经在regex101上验证了它的工作原理。
文档Map:
{
"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
}
}
}
}
型
回复:
{
"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": []
}
]
}
}
型
我确定有匹配的数据。因为当我用"prefix": "iphone 5"
替换"regex": "/iphone 5.*/gi"
时,接口返回的数据。
{
"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"
]
}
}
型
但是正如你所看到的,在用"prefix": "iphone 5"
替换它之后,它返回了无关的数据,这就是为什么我想使用正则表达式。
1条答案
按热度按时间7qhs6swi1#
当查看Elasticsearch的regexp编译器(https://www.elastic.co/guide/en/elasticsearch/reference/8.11/query-dsl-regexp-query.html和https://www.elastic.co/guide/en/elasticsearch/reference/8.11/regexp-syntax.html)文档时,似乎你的regex语法不是Elasticsearch所期望的。