代码之家  ›  专栏  ›  技术社区  ›  Rodrick Zadrozny

在补全提示中使用正则表达式时不起作用

  •  0
  • Rodrick Zadrozny  · 技术社区  · 2 年前

    我想查找所有以“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” ,它返回不相关的数据,这就是我想使用正则表达式的原因。

    0 回复  |  直到 2 年前
        1
  •  0
  •   Abacus    2 年前

    在查看Elasticsearch文档中的正则表达式建议时( 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 )看起来你的正则表达式语法不是Elasticsearch所期望的。

    推荐文章