【问题标题】:ElasticSearch autocompleteElasticSearch 自动完成
【发布时间】:2014-07-22 08:18:21
【问题描述】:

我有四个文档,其中包含一个名为“fullname”的字段。

文件:

  • 阿比盖尔·哈里森
  • 阿比盖尔·哈迪森
  • 阿比琳哈维顿
  • 阿比林-哈文顿

我想为此字段创建一个自动完成程序。一些例子:

搜索:“阿比” 结果:“Abigail Harrison”、“Abigale Hardison”、“Abilene Haveton”

搜索:“Abig” 结果:“Abigail Harrison”、“Abigale Hardison”

搜索:“阿比盖尔·哈尔” 结果:“Abigail Harrison”、“Abigale Hardison”

搜索:“Abilene Hav” 结果:“Abilene Haveton”、“Abilene-Havington”

搜索:“哈” 结果:“Abigail Harrison”、“Abigale Hardison”

我不想要这样的东西:(!)

搜索:“iga” 结果:“Abigail Harrison”、“Abigale Hardison”

应该忽略空格和连字符,我希望所有生成的标记都小写,因此搜索查询不应该区分大小写。

我的 ES 设置如下。

{
"mappings": {
    "person": {
        "properties": {
            "fullname": {
                "index": "analyzed",
                "index_analyzer": "autocomplete",
                "search_analyzer": "standard",
                "type": "string"
            }
        }
    }
},
"settings": {
    "index": {
        "analysis": {
            "analyzer": {
                "autocomplete": {
                    "filter": [
                        "lowercase",
                        "edgengram"
                    ],
                    "tokenizer": "whitespace"
                }
            },
            "filter": {
                "edgengram": {
                    "max_gram": 50,
                    "min_gram": 3,
                    "type": "edgeNGram"
                }
            }
        }
    }
}

}

【问题讨论】:

  • 这似乎是什么问题?发生了什么出乎意料的事情?

标签: search lucene elasticsearch


【解决方案1】:

在建立索引时,您应该使用标准标记器以及小写、asciifolding、suggest_shingle、edgengram,并在搜索时使用关键字分析器。

尝试使用类似的东西:

"index":{
"analysis": {
    "analyzer": {
        "autocomplete": {
            "tokenizer": "standard",
            "filter": [
                "lowercase",
                "asciifolding",
                "suggestions_shingle",
                "edgengram"
            ]
        }
    },
    "filter": {
        "suggestions_shingle": {
            "type": "shingle",
            "min_shingle_size": 2,
            "max_shingle_size": 5
        },
        "edgengram": {
            "type": "edgeNGram",
            "min_gram": 2,
            "max_gram": 30,
            "side": "front"
        }
    }
}
}

"mappings": {
    "person": {
        "properties": {
            "fullname": {
                "index": "analyzed",
                "index_analyzer": "autocomplete",
                "search_analyzer": "keyword",
                "type": "string"
            }
        }
    }
}

然后尝试使用匹配查询进行搜索。它应该可以解决您的问题。

谢谢

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-03-05
    • 2020-07-14
    • 2016-02-14
    • 2017-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多