常用词组词元过滤器
common_grams
词元过滤器通过保留文本中常用词组(common grams)来提高搜索相关性。这在处理某些词组经常作为一个单元出现,如果被视为单独的词元会影响搜索相关性的语言或数据集时非常有用。如果输入字符串中存在任何常用词,此词元过滤器会生成它们的 unigram(一元词)和 bigram(二元词)。
使用此词元过滤器可以通过保持常用词组的完整性来提高搜索相关性。这有助于更准确地匹配查询,特别是对于频繁的词组组合。它还通过减少不相关的匹配来提高搜索精度。
使用此过滤器时,必须仔细选择和维护 common_words
列表。
参数
common_grams
词元过滤器可以使用以下参数进行配置。
参数 | 必需/可选 | 数据类型 | 描述 |
---|---|---|---|
common_words | 必需 | 字符串列表 | 应被视为常用词的词列表。这些词将用于生成常用词组。如果 common_words 参数为空列表,则 common_grams 词元过滤器将成为一个无操作(no-op)过滤器,这意味着它根本不会修改输入词元。 |
ignore_case | 可选 | 布尔型 | 指示过滤器在匹配常用词时是否应忽略大小写差异。默认值为 false 。 |
query_mode | 可选 | 布尔型 | 当设置为 true 时,应用以下规则- 从 common_words 生成的 unigram 不会包含在输出中。- 非常用词后跟常用词的 bigram 会保留在输出中。 - 如果非常用词的 unigram 紧跟常用词,则会被排除。 - 如果非常用词出现在文本末尾且前面是常用词,则其 unigram 不会包含在输出中。 |
示例
以下示例请求创建一个名为 my_common_grams_index
的新索引,并使用 common_grams
过滤器配置一个分析器
PUT /my_common_grams_index
{
"settings": {
"analysis": {
"filter": {
"my_common_grams_filter": {
"type": "common_grams",
"common_words": ["a", "in", "for"],
"ignore_case": true,
"query_mode": true
}
},
"analyzer": {
"my_analyzer": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"my_common_grams_filter"
]
}
}
}
}
}
生成的词元
使用以下请求检查使用该分析器生成的词元
GET /my_common_grams_index/_analyze
{
"analyzer": "my_analyzer",
"text": "A quick black cat jumps over the lazy dog in the park"
}
响应包含生成的词元
{
"tokens": [
{"token": "a_quick","start_offset": 0,"end_offset": 7,"type": "gram","position": 0},
{"token": "quick","start_offset": 2,"end_offset": 7,"type": "<ALPHANUM>","position": 1},
{"token": "black","start_offset": 8,"end_offset": 13,"type": "<ALPHANUM>","position": 2},
{"token": "cat","start_offset": 14,"end_offset": 17,"type": "<ALPHANUM>","position": 3},
{"token": "jumps","start_offset": 18,"end_offset": 23,"type": "<ALPHANUM>","position": 4},
{"token": "over","start_offset": 24,"end_offset": 28,"type": "<ALPHANUM>","position": 5},
{"token": "the","start_offset": 29,"end_offset": 32,"type": "<ALPHANUM>","position": 6},
{"token": "lazy","start_offset": 33,"end_offset": 37,"type": "<ALPHANUM>","position": 7},
{"token": "dog_in","start_offset": 38,"end_offset": 44,"type": "gram","position": 8},
{"token": "in_the","start_offset": 42,"end_offset": 48,"type": "gram","position": 9},
{"token": "the","start_offset": 45,"end_offset": 48,"type": "<ALPHANUM>","position": 10},
{"token": "park","start_offset": 49,"end_offset": 53,"type": "<ALPHANUM>","position": 11}
]
}