看看shingles TokenFilter (documentation)。它与ngram 非常相似,但使用标记而不是字符。
使用默认设置,它会生成两个字长的标记。您可以使用 _analyze API 检查其行为:
POST _analyze?tokenizer=whitespace&filters=shingle&text=The mouse is very little
这将输出:
{
"tokens": [
{
"token": "The",
"start_offset": 0,
"end_offset": 3,
"type": "word",
"position": 1
},
{
"token": "The mouse",
"start_offset": 0,
"end_offset": 9,
"type": "shingle",
"position": 1
},
{
"token": "mouse",
"start_offset": 4,
"end_offset": 9,
"type": "word",
"position": 2
},
{
"token": "mouse is",
"start_offset": 4,
"end_offset": 12,
"type": "shingle",
"position": 2
},
{
"token": "is",
"start_offset": 10,
"end_offset": 12,
"type": "word",
"position": 3
},
{
"token": "is very",
"start_offset": 10,
"end_offset": 17,
"type": "shingle",
"position": 3
},
{
"token": "very",
"start_offset": 13,
"end_offset": 17,
"type": "word",
"position": 4
},
{
"token": "very little",
"start_offset": 13,
"end_offset": 24,
"type": "shingle",
"position": 4
},
{
"token": "little",
"start_offset": 18,
"end_offset": 24,
"type": "word",
"position": 5
}
]
}
然后,通过查询该字段,您将看到两个示例文档之间的差异。
您可以在权威指南的this section 中找到有关邻近搜索的详细说明。