我目前正在我的应用程序中实现elasticsearch。请假定“ Hello World”是我们需要搜索的数据。我们的要求是我们应该通过输入“ h”或“ Hello World”或“ Hello Worlds”作为关键字来获得结果。
这是我们当前的查询。
{
"query": {
"wildcard" : {
"message" : {
"title" : "h*"
}
}
}
}
通过使用此选项,我们使用关键字“ h”可获得正确的结果。但是,如果拼写错误很小,我们也需要得到结果。
您需要使用英语分析器,该分析器将令牌标记成其根形式。更多信息可以在这里找到
我通过使用边缘n-gram分析器和match query来获取示例数据,查询和预期结果来实现它。
{
"settings": {
"analysis": {
"filter": {
"autocomplete_filter": {
"type": "edge_ngram",
"min_gram": 1,
"max_gram": 10
}
},
"analyzer": {
"autocomplete": {
"type": "custom",
"tokenizer": "standard",
"filter": [
"lowercase",
"autocomplete_filter"
]
}
}
}
},
"mappings": {
"properties": {
"title": {
"type": "text",
"analyzer": "autocomplete",
"search_analyzer": "english"
}
}
}
}
{
"title" : "Hello World"
}
h
及其结果{
"query": {
"match": {
"title": "h"
}
}
}
"hits": [
{
"_index": "so-60524477-partial-key",
"_type": "_doc",
"_id": "1",
"_score": 0.42763555,
"_source": {
"title": "Hello World"
}
}
]
Hello Worlds
并得到相同的文件{
"query": {
"match": {
"title": "Hello worlds"
}
}
}
结果
"hits": [
{
"_index": "so-60524477-partial-key",
"_type": "_doc",
"_id": "1",
"_score": 0.8552711,
"_source": {
"title": "Hello World"
}
}
]
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句