N-gram 词元过滤器
ngram
词元过滤器是一个强大的工具,用于将文本分解为更小的组件,称为 n-gram,可以提高部分匹配和模糊搜索能力。它通过将词元拆分为指定长度的更小子字符串来工作。这些过滤器常用于搜索应用程序,以支持自动完成、部分匹配和容错性搜索。更多信息,请参阅自动完成功能和“您是否想搜…”功能。
参数
ngram
词元过滤器可以通过以下参数进行配置。
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
min_gram | 可选 | 整数 | n-gram 的最小长度。默认为 1 。 |
max_gram | 可选 | 整数 | n-gram 的最大长度。默认为 2 。 |
preserve_original | 可选 | 布尔型 | 是否保留原始词元作为输出之一。默认为 false 。 |
示例
以下示例请求创建一个名为 ngram_example_index
的新索引,并配置一个带有 ngram
过滤器的分析器
PUT /ngram_example_index
{
"settings": {
"analysis": {
"filter": {
"ngram_filter": {
"type": "ngram",
"min_gram": 2,
"max_gram": 3
}
},
"analyzer": {
"ngram_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"ngram_filter"
]
}
}
}
}
}
生成的词元
使用以下请求检查使用该分析器生成的词元
POST /ngram_example_index/_analyze
{
"analyzer": "ngram_analyzer",
"text": "Search"
}
响应包含生成的词元
{
"tokens": [
{
"token": "se",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "sea",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "ea",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "ear",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "ar",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "arc",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "rc",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "rch",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
},
{
"token": "ch",
"start_offset": 0,
"end_offset": 6,
"type": "<ALPHANUM>",
"position": 0
}
]
}