Elasticsearch在Analyzer中使用字符过滤器来删除“?”之后的所有字符。

时间:2018-10-21 07:48:43

标签: elasticsearch elastic-stack

在elasticsearch中,我尝试在一个字段上使用分析器,该分析器将使用过滤器替换?后的所有字符。遇到空格。为此,我正在使用以下过滤器。

"filter_name":{
  "type": "pattern_replace",
  "pattern": "\\?(.*)",
  "replacement": ""
}

但这不能按预期工作。有什么我想念的吗?

2 个答案:

答案 0 :(得分:0)

使用模式:"(?<=\\?)(.*)"和替换项:""

答案 1 :(得分:0)

请参阅以下内容。我创建了一个示例映射和一个示例_analyze查询,以了解如何创建令牌。

映射

PUT my_index
{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "tokenizer": "standard",
          "char_filter": [
            "my_char_filter"
          ]
        }
      },
      "char_filter": {
        "my_char_filter": {
          "type": "pattern_replace",
          "pattern": "(?=.*)\\?(.*)",
          "replacement": ""
        }
      }
    }
  }
}

查询

POST my_index/_analyze
{
  "analyzer": "my_analyzer",
  "text": "Do you know? Life is crazy"
}

分析结果

{
  "tokens": [
    {
      "token": "Do",
      "start_offset": 0,
      "end_offset": 2,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "you",
      "start_offset": 3,
      "end_offset": 6,
      "type": "<ALPHANUM>",
      "position": 1
    },
    {
      "token": "know",
      "start_offset": 7,
      "end_offset": 26,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}

希望这会有所帮助!