字符组分词器
char_group
分词器使用特定字符作为分隔符将文本拆分为词元。它适用于需要直接分词的场景,提供了比基于模式的分词器更简单的替代方案,且没有额外的复杂性。
使用示例
以下示例请求创建一个名为 my_index
的新索引,并使用 char_group
分词器配置分析器。该分词器根据空格、-
和 :
字符来拆分文本
PUT /my_index
{
"settings": {
"analysis": {
"tokenizer": {
"my_char_group_tokenizer": {
"type": "char_group",
"tokenize_on_chars": [
"whitespace",
"-",
":"
]
}
},
"analyzer": {
"my_char_group_analyzer": {
"type": "custom",
"tokenizer": "my_char_group_tokenizer"
}
}
}
},
"mappings": {
"properties": {
"content": {
"type": "text",
"analyzer": "my_char_group_analyzer"
}
}
}
}
生成的词元
使用以下请求检查使用该分析器生成的词元
POST /my_index/_analyze
{
"analyzer": "my_char_group_analyzer",
"text": "Fast-driving cars: they drive fast!"
}
响应包含生成的词元
{
"tokens": [
{
"token": "Fast",
"start_offset": 0,
"end_offset": 4,
"type": "word",
"position": 0
},
{
"token": "driving",
"start_offset": 5,
"end_offset": 12,
"type": "word",
"position": 1
},
{
"token": "cars",
"start_offset": 13,
"end_offset": 17,
"type": "word",
"position": 2
},
{
"token": "they",
"start_offset": 19,
"end_offset": 23,
"type": "word",
"position": 3
},
{
"token": "drive",
"start_offset": 24,
"end_offset": 29,
"type": "word",
"position": 4
},
{
"token": "fast!",
"start_offset": 30,
"end_offset": 35,
"type": "word",
"position": 5
}
]
}
参数
char_group
分词器可以通过以下参数进行配置。
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
tokenize_on_chars | 必需 | 数组 | 指定应根据其进行文本分词的字符集。您可以指定单个字符(例如,- 或 @ ),包括转义字符(例如,\n ),或字符类别,例如 whitespace 、letter 、digit 、punctuation 或 symbol 。 |
max_token_length | 可选 | 整数 | 设置生成词元的最大长度。如果超出此长度,词元将按 max_token_length 中配置的长度拆分为多个词元。默认值为 255 。 |