总体效果相似百度搜索,如图:html
1.创建索引算法
PUT /book4 { "mappings": { "english": { "properties": { "passage": { "type": "text" } } } } }
curl -H "Content-Type: application/json" -XPOST 'http:localhost:9200/_bulk' -d' { "index" : { "_index" : "book4", "_type" : "english" } } { "passage": "Lucene is cool"} { "index" : { "_index" : "book4", "_type" : "english" } } { "passage": "Elasticsearch builds on top of lucene"} { "index" : { "_index" : "book4", "_type" : "english" } } { "passage": "Elasticsearch rocks"} { "index" : { "_index" : "book4", "_type" : "english" } } { "passage": "Elastic is the company behind ELK stack"} { "index" : { "_index" : "book4", "_type" : "english" } } { "passage": "elk rocks"} { "index" : { "_index" : "book4", "_type" : "english" } } { "passage": "elasticsearch is rock solid"} '
post /_analyze { "text": [ "Lucene is cool", "Elasticsearch builds on top of lucene", "Elasticsearch rocks", "Elastic is the company behind ELK stack", "elk rocks", "elasticsearch is rock solid" ] }
结果:json
{ "tokens": [ { "token": "lucene", "start_offset": 0, "end_offset": 6, "type": "<ALPHANUM>", "position": 0 }, { "token": "is", "start_offset": 7, "end_offset": 9, "type": "<ALPHANUM>", "position": 1 }, { "token": "cool", "start_offset": 10, "end_offset": 14, "type": "<ALPHANUM>", "position": 2 }, { "token": "elasticsearch", "start_offset": 15, "end_offset": 28, "type": "<ALPHANUM>", "position": 103 }, { "token": "builds", "start_offset": 29, "end_offset": 35, "type": "<ALPHANUM>", "position": 104 }, { "token": "on", "start_offset": 36, "end_offset": 38, "type": "<ALPHANUM>", "position": 105 }, { "token": "top", "start_offset": 39, "end_offset": 42, "type": "<ALPHANUM>", "position": 106 }, { "token": "of", "start_offset": 43, "end_offset": 45, "type": "<ALPHANUM>", "position": 107 }, { "token": "lucene", "start_offset": 46, "end_offset": 52, "type": "<ALPHANUM>", "position": 108 }, { "token": "elasticsearch", "start_offset": 53, "end_offset": 66, "type": "<ALPHANUM>", "position": 209 }, { "token": "rocks", "start_offset": 67, "end_offset": 72, "type": "<ALPHANUM>", "position": 210 }, { "token": "elastic", "start_offset": 73, "end_offset": 80, "type": "<ALPHANUM>", "position": 311 }, { "token": "is", "start_offset": 81, "end_offset": 83, "type": "<ALPHANUM>", "position": 312 }, { "token": "the", "start_offset": 84, "end_offset": 87, "type": "<ALPHANUM>", "position": 313 }, { "token": "company", "start_offset": 88, "end_offset": 95, "type": "<ALPHANUM>", "position": 314 }, { "token": "behind", "start_offset": 96, "end_offset": 102, "type": "<ALPHANUM>", "position": 315 }, { "token": "elk", "start_offset": 103, "end_offset": 106, "type": "<ALPHANUM>", "position": 316 }, { "token": "stack", "start_offset": 107, "end_offset": 112, "type": "<ALPHANUM>", "position": 317 }, { "token": "elk", "start_offset": 113, "end_offset": 116, "type": "<ALPHANUM>", "position": 418 }, { "token": "rocks", "start_offset": 117, "end_offset": 122, "type": "<ALPHANUM>", "position": 419 }, { "token": "elasticsearch", "start_offset": 123, "end_offset": 136, "type": "<ALPHANUM>", "position": 520 }, { "token": "is", "start_offset": 137, "end_offset": 139, "type": "<ALPHANUM>", "position": 521 }, { "token": "rock", "start_offset": 140, "end_offset": 144, "type": "<ALPHANUM>", "position": 522 }, { "token": "solid", "start_offset": 145, "end_offset": 150, "type": "<ALPHANUM>", "position": 523 } ] }
搜索下试试,给出错误单词Elasticsearaach后端
POST /book4/_search { "suggest" : { "my-suggestion" : { "text" : "Elasticsearaach", "term" : { "field" : "passage",
"suggest_mode": "popular" } } } }
response:api
{ "took": 26, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": 0, "hits": [] }, "suggest": { "my-suggestion": [ { "text": "elasticsearaach", "offset": 0, "length": 15, "options": [ { "text": "elasticsearch", "score": 0.84615386, "freq": 3 } ] } ] } }
POST _search { "suggest": { "my-suggest-1" : { "text" : "tring out Elasticsearch", "term" : { "field" : "message" } }, "my-suggest-2" : { "text" : "kmichy", "term" : { "field" : "user" } } } }
该term
建议者提出基于编辑距离条款。在建议术语以前分析提供的建议文本。建议的术语是根据分析的建议文本标记提供的。该term
建议者不走查询到的是是的请求部分。数据结构
|
建议文字。建议文本是必需的选项,须要全局或按建议设置。curl |
|
从中获取候选建议的字段。这是一个必需的选项,须要全局设置或根据建议设置。ide |
|
用于分析建议文本的分析器。默认为建议字段的搜索分析器。 |
|
每一个建议文本标记返回的最大更正。 |
|
定义如何根据建议文本术语对建议进行排序。两个可能的值:
|
|
建议模式控制包含哪些建议或控制建议的文本术语,建议。能够指定三个可能的值:
|
|
在文本分析以后,建议文本术语小写。 |
|
最大编辑距离候选建议能够具备以便被视为建议。只能是介于1和2之间的值。任何其余值都会致使抛出错误的请求错误。默认为2。 |
|
必须匹配的最小前缀字符的数量才是候选建议。默认为1.增长此数字可提升拼写检查性能。一般拼写错误不会出如今术语的开头。(旧名“prefix_len”已弃用) |
|
建议文本术语必须具备的最小长度才能包含在内。默认为4.(旧名称“min_word_len”已弃用) |
|
设置从每一个单独分片中检索的最大建议数。在减小阶段,仅根据 |
|
用于乘以的因子, |
|
建议应出现的文档数量的最小阈值。能够指定为绝对数字或文档数量的相对百分比。这能够仅经过建议高频项来提升质量。默认为0f且未启用。若是指定的值大于1,则该数字不能是小数。分片级文档频率用于此选项。 |
|
建议文本令牌能够存在的文档数量的最大阈值,以便包括在内。能够是表示文档频率的相对百分比数(例如0.4)或绝对数。若是指定的值大于1,则不能指定小数。默认为0.01f。这可用于排除高频术语的拼写检查。高频术语一般拼写正确,这也提升了拼写检查的性能。分片级文档频率用于此选项。 |
|
用于比较相似建议术语的字符串距离实现。能够指定五个可能的值: |
phrase 短语建议,在term的基础上,会考量多个term之间的关系,好比是否同时出如今索引的原文里,相邻程度,以及词频等
示例1:
POST book4/_search
{ "took": 11, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": 0, "hits": [] }, "suggest": { "myss": [ { "text": "Elasticsearch rock", "offset": 0, "length": 18, "options": [ { "text": "elasticsearch rocks", "score": 0.3467123 } ] } ] } }
针对自动补全场景而设计的建议器。此场景下用户每输入一个字符的时候,就须要即时发送一次查询请求到后端查找匹配项,在用户输入速度较高的状况下对后端响应速度要求比较苛刻。所以实现上它和前面两个Suggester采用了不一样的数据结构,索引并不是经过倒排来完成,而是将analyze过的数据编码成FST和索引一块儿存放。对于一个open状态的索引,FST会被ES整个装载到内存里的,进行前缀查找速度极快。可是FST只能用于前缀查找,这也是Completion Suggester的局限所在。
POST /book5 { "mappings": { "music" : { "properties" : { "suggest" : { "type" : "completion" }, "title" : { "type": "keyword" } } } } }
插入数据:
POST /book5/music { "suggest":"test my book" }
Input 指定输入词 Weight 指定排序值(可选)
PUT music/music/5nupmmUBYLvVFwGWH3cu?refresh { "suggest" : { "input": [ "test", "book" ], "weight" : 34 } }
指定不一样的排序值:
PUT music/_doc/6Hu2mmUBYLvVFwGWxXef?refresh { "suggest" : [ { "input": "test", "weight" : 10 }, { "input": "good", "weight" : 3 } ]}
POST book5/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "te", "completion" : { "field" : "suggest" } } } }
{ "took": 8, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": 0, "hits": [] }, "suggest": { "song-suggest": [ { "text": "te", "offset": 0, "length": 2, "options": [ { "text": "test my book1", "_index": "book5", "_type": "music", "_id": "6Xu6mmUBYLvVFwGWpXeL", "_score": 1, "_source": { "suggest": "test my book1" } }, { "text": "test my book1", "_index": "book5", "_type": "music", "_id": "6nu8mmUBYLvVFwGWSndC", "_score": 1, "_source": { "suggest": "test my book1" } }, { "text": "test my book1 english", "_index": "book5", "_type": "music", "_id": "63u8mmUBYLvVFwGWZHdC", "_score": 1, "_source": { "suggest": "test my book1 english" } } ] } ] } }
{ "suggest": { "song-suggest" : { "prefix" : "te", "completion" : { "field" : "suggest" , "skip_duplicates": true } } } }
POST /book5/music/63u8mmUBYLvVFwGWZHdC?refresh { "suggest" : { "input": [ "book1 english", "test english" ], "weight" : 20 } }
查询:
POST book5/_search?pretty { "suggest": { "song-suggest" : { "prefix" : "test", "completion" : { "field" : "suggest" , "skip_duplicates": true } } } }
结果:
{ "took": 7, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 0, "max_score": 0, "hits": [] }, "suggest": { "song-suggest": [ { "text": "test", "offset": 0, "length": 4, "options": [ { "text": "test english", "_index": "book5", "_type": "music", "_id": "63u8mmUBYLvVFwGWZHdC", "_score": 20, "_source": { "suggest": { "input": [ "book1 english", "test english" ], "weight": 20 } } }, { "text": "test my book1", "_index": "book5", "_type": "music", "_id": "6Xu6mmUBYLvVFwGWpXeL", "_score": 1, "_source": { "suggest": "test my book1" } } ] } ] } }
所以用好Completion Sugester并非一件容易的事,实际应用开发过程当中,须要根据数据特性和业务须要,灵活搭配analyzer和mapping参数,反复调试才可能得到理想的补全效果。
回到篇首搜索框的补全/纠错功能,若是用ES怎么实现呢?我能想到的一个的实现方式:
精准程度上(Precision)看: Completion > Phrase > term, 而召回率上(Recall)则反之。从性能上看,Completion Suggester是最快的,若是能知足业务需求,只用Completion Suggester作前缀匹配是最理想的。 Phrase和Term因为是作倒排索引的搜索,相比较而言性能应该要低很多,应尽可能控制suggester用到的索引的数据量,最理想的情况是通过必定时间预热后,索引能够全量map到内存。