整体效果类似百度搜索,如图:
针对自动补全场景而设计的建议器。此场景下用户每输入一个字符的时候,就需要即时发送一次查询请求到后端查找匹配项,在用户输入速度较高的情况下对后端响应速度要求比较苛刻。因此实现上它和前面两个Suggester采用了不同的数据结构,索引并非通过倒排来完成,而是将analyze过的数据编码成FST和索引一起存放。对于一个open状态的索引,FST会被ES整个装载到内存里的,进行前缀查找速度极快。但是FST只能用于前缀查找,这也是Completion Suggester的局限所在。
- put /book
-
- {
- "mappings": {
- "music" : {
- "properties" : {
- "suggest" : {
- "type" : "completion"
- },
- "title" : {
- "type": "keyword"
- }
- }
- }
- }
- }
-
- 注意,最终写入数据需要 /book/music
- put /book/music/_doc/1
- {
- "suggest":"爱一个人好难"
- }
-
- put /book/music/_doc/2
- {
- "suggest":"爱一个人好难"
- }
-
- put /book/music/_doc/3
- {
- "suggest":"爱真的需要勇气"
- }
示例1:查询建议根据前缀查询
- POST book/music/_search
- {
- "suggest": {
- "song-suggest" : {
- "prefix" : "爱",
- "completion" : {
- "field" : "suggest"
- }
- }
- }
- }
-
- {
- "took": ,
- "timed_out": false,
- "_shards": {
- "total": ,
- "successful": ,
- "skipped": ,
- "failed":
- },
- "hits": {
- "total": ,
- "max_score": ,
- "hits": []
- },
- "suggest": {
- "song-suggest": [
- {
- "text": "te",
- "offset": ,
- "length": ,
- "options": [
- {
- "text": "爱一个人好难",
- "_index": "book",
- "_type": "music",
- "_id": "6Xu6mmUBYLvVFwGWpXeL",
- "_score": ,
- "_source": {
- "suggest": "爱一个人好难"
- }
- },
- {
- "text": "爱一个人好难",
- "_index": "book",
- "_type": "music",
- "_id": "6nu8mmUBYLvVFwGWSndC",
- "_score": ,
- "_source": {
- "suggest": "爱一个人好难"
- }
- },
- {
- "text": "爱真的需要勇气",
- "_index": "book",
- "_type": "music",
- "_id": "63u8mmUBYLvVFwGWZHdC",
- "_score": ,
- "_source": {
- "suggest": "爱真的需要勇气"
- }
- }
- ]
- }
- ]
- }
- }
- {
- "suggest": {
- "song-suggest" : {
- "prefix" : "爱",
- "completion" : {
- "field" : "suggest" ,
- "skip_duplicates": true
- }
- }
- }
- }
本文部分参考:《ES系列十三、Elasticsearch Suggester API(自动补全)》 里面有整个四种补全的方式。