elasticsearch 正则表达式在completion-receiver中使用时不起作用

lyr7nygr  于 5个月前  发布在  ElasticSearch
关注(0)|答案(1)|浏览(37)

我想查找所有以“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"替换它之后,它返回了无关的数据,这就是为什么我想使用正则表达式。

相关问题