锚标记替换模式到标记的末尾

Anchor token replace patterns to the end of tokens

根据 docs 这应该是不可能的

Regular expressions cannot be anchored to the beginning or end of a token

不过它似乎对我有用

GET /_analyze
{
  "tokenizer": "whitespace",
  "filter": [
    {
      "type": "pattern_replace",
      "pattern": "(dog)$",
      "replacement": "hot"
    }
  ],
  "text": "dog dogs"
}

returns

{
  "tokens" : [
    {
      "token" : "hotdog",
      "start_offset" : 0,
      "end_offset" : 3,
      "type" : "word",
      "position" : 0
    },
    {
      "token" : "dogs",
      "start_offset" : 4,
      "end_offset" : 8,
      "type" : "word",
      "position" : 1
    }
  ]
}

请注意,该模式锚定到令牌的末尾,并且“dogs”未被替换,因为它不以“dog”结尾。

所以我的问题是:我是否遗漏了什么或者我可以安全地使用它(而且文档是错误的)?

看起来是错误的文档,Elasticsearch bug为此,查看了弹性代码,没有对令牌的开头或结尾进行特殊处理.

请参考this ES code这个token filter使用的,它调用了Lucene token filter,在Elastic和Lucene代码层面都没有特殊处理。