条件词元过滤器
condition
词元过滤器是一种特殊类型的过滤器,它允许您根据特定条件有条件地应用其他词元过滤器。这使您能够更好地控制在文本分析过程中何时应用某些词元过滤器。可以配置多个过滤器,并且只有在满足您定义的条件时才会应用它们。此词元过滤器对于语言特定的处理和特殊字符的处理非常有用。
参数
要使用 condition
词元过滤器,必须配置两个参数。
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
filter | 必需 | 数组 | 指定当满足特定条件(由 script 参数定义)时,应将哪些词元过滤器应用于词元。 |
script(脚本) | 必需 | 对象 | 配置一个内联脚本,该脚本定义了应用 filter 参数中指定的过滤器的所需条件(只接受内联脚本)。 |
示例
以下示例请求创建一个名为 my_conditional_index
的新索引,并配置一个带有 condition
过滤器的分析器。此过滤器将 lowercase
过滤器应用于任何包含字符序列“um”的词元。
PUT /my_conditional_index
{
"settings": {
"analysis": {
"filter": {
"my_conditional_filter": {
"type": "condition",
"filter": ["lowercase"],
"script": {
"source": "token.getTerm().toString().contains('um')"
}
}
},
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"my_conditional_filter"
]
}
}
}
}
}
生成的词元
使用以下请求检查使用该分析器生成的词元
GET /my_conditional_index/_analyze
{
"analyzer": "my_analyzer",
"text": "THE BLACK CAT JUMPS OVER A LAZY DOG"
}
响应包含生成的词元
{
"tokens": [
{
"token": "THE",
"start_offset": 0,
"end_offset": 3,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "BLACK",
"start_offset": 4,
"end_offset": 9,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "CAT",
"start_offset": 10,
"end_offset": 13,
"type": "<ALPHANUM>",
"position": 2
},
{
"token": "jumps",
"start_offset": 14,
"end_offset": 19,
"type": "<ALPHANUM>",
"position": 3
},
{
"token": "OVER",
"start_offset": 20,
"end_offset": 24,
"type": "<ALPHANUM>",
"position": 4
},
{
"token": "A",
"start_offset": 25,
"end_offset": 26,
"type": "<ALPHANUM>",
"position": 5
},
{
"token": "LAZY",
"start_offset": 27,
"end_offset": 31,
"type": "<ALPHANUM>",
"position": 6
},
{
"token": "DOG",
"start_offset": 32,
"end_offset": 35,
"type": "<ALPHANUM>",
"position": 7
}
]
}