保留词元过滤器
keep_words
词元过滤器旨在仅在分析过程中保留某些词语。如果您有大量文本但只对某些关键字或词项感兴趣,此过滤器将非常有用。
参数
keep_words
词元过滤器可以使用以下参数进行配置。
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
keep_words | 如果未配置 keep_words_path ,则此项为必填 | 字符串列表 | 要保留的词语列表。 |
keep_words_path | 如果未配置 keep_words ,则此项为必填 | 字符串 | 包含要保留的词语列表的文件的路径。 |
keep_words_case | 可选 | 布尔型 | 在比较时是否将所有词语转换为小写。默认值为 false 。 |
示例
以下示例请求创建一个名为 my_index
的新索引,并配置一个带有 keep_words
过滤器的分析器。
PUT my_index
{
"settings": {
"analysis": {
"analyzer": {
"custom_keep_word": {
"tokenizer": "standard",
"filter": [ "keep_words_filter" ]
}
},
"filter": {
"keep_words_filter": {
"type": "keep",
"keep_words": ["example", "world", "opensearch"],
"keep_words_case": true
}
}
}
}
}
生成的词元
使用以下请求检查使用该分析器生成的词元
GET /my_index/_analyze
{
"analyzer": "custom_keep_word",
"text": "Hello, world! This is an OpenSearch example."
}
响应包含生成的词元
{
"tokens": [
{
"token": "world",
"start_offset": 7,
"end_offset": 12,
"type": "<ALPHANUM>",
"position": 1
},
{
"token": "OpenSearch",
"start_offset": 25,
"end_offset": 35,
"type": "<ALPHANUM>",
"position": 5
},
{
"token": "example",
"start_offset": 36,
"end_offset": 43,
"type": "<ALPHANUM>",
"position": 6
}
]
}