elasticsearch:用于匹配autocomplete suggester中字符串列表中最长字符串的regex

pieyvz9o  于 2021-06-15  发布在  ElasticSearch
关注(0)|答案(1)|浏览(429)

我对ElasticSearch非常陌生,尝试用regex查询实现autocomplete suggester。一旦我收到了一个查询,我就用查询的最后5个单词,并以此格式形成一个令牌列表

query - i am trying regex for elasticsearch
tokens - [elasticsearch, for elasticsearch, regex for elasticsearch ...]

我的要求是识别索引语句,它与标记列表中最长的字符串相匹配我很难为它编写正则表达式。有人能帮忙吗?
我的Map:

"mappings": {
"properties": {
  "keywords": {
    "type": "text",
    "fields": {
      "keywords_suggest": {
        "type": "completion"
      }
    }
  },
  "sections": {
    "type": "text",
    "fields": {
      "sections_suggest": {
        "type": "completion"
      }
    }
  },
  "title": {
    "type": "text",
    "fields": {
      "title_suggest": {
        "type": "completion"
      }
    }
  }

这就是我提出搜索请求的方式

body = {
        "from": 0, "size": size,
        "query": {
            "multi_match": {
                "query": query,
                "fields": ["title^3", "searchResultPreview^1", "body^5"], #ignore these fields as i only pasted mapping used for completion type
                "fuzziness": "AUTO"
            }
        },
        "suggest": {
            "title-suggest": {
                "regex": regex,
                "completion": {
                    "field": "title.title_suggest",
                    "skip_duplicates": True,
                }
            },
            "keyword-suggest": {
                "regex": regex,
                "completion": {
                    "field": "keywords.keywords_suggest",
                    "skip_duplicates": True,
                }
            },
            "section-suggest": {
                "regex": regex,
                "completion": {
                    "field": "sections.sections_suggest",
                    "skip_duplicates": True,
                }
            }
        }
    }

  search_result = self.es.search(index=index_name, body=body)
Indexed sentence1 - The Real purpose of elasticsearch is unknown
Indexed sentence2 - real function is not defined
query - i want to know the real
list of words - [ real, the real, know the real, to know the real]

我试着遵循正则表达式- (to know the real|know the real|the real|real) 必需的输出索引语句1需要匹配,因为它是列表中句子开头的最长单词,但它只显示与以real开头的句子匹配的结果
有人能告诉我,我哪里做错了。
编辑:我相信区分大小写不是问题,因为单词real的匹配不区分大小写

hl0ma9xz

hl0ma9xz1#

正则表达式区分大小写
在匹配组之前使用(?i),它应该有效
看到它在这里工作了吗

相关问题