1-gram 建议开箱即用,2-gram 建议可以通过shingle 轻松实现。
这是一个尝试
PUT test
{
"settings": {
"analysis": {
"analyzer": {
"2-grams": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"shingles"
]
}
},
"filter": {
"shingles": {
"type": "shingle",
"min_shingle_size": 2,
"max_shingle_size": 2,
"output_unigrams": false
}
}
}
},
"mappings": {
"properties": {
"text": {
"type": "text",
"analyzer": "standard",
"fields": {
"2gram": {
"type": "text",
"analyzer": "2-grams"
}
}
}
}
}
}
接下来索引一些文档:
PUT test/_doc/1
{
"text": "Two pees in a pot"
}
PUT test/_doc/2
{
"text": "A Watched pot tastes bitter"
}
最后,您可以使用以下查询搜索 1-gram 建议,您将在响应中获得两个文档:
POST test/_search
{
"query": {
"match": {
"text": "A watched pot never boils"
}
}
}
您还可以使用以下查询搜索 2-gram 建议,并且只会出现第二个文档:
POST test/_search
{
"query": {
"match": {
"text.2gram": "A watched pot never boils"
}
}
}
PS:虽然不确定“分析”建议的工作原理,但请随时提供更多见解,我会更新。