ES系列Elasticsearch Suggester API(自动补全)

Posted 悟能的师兄

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ES系列Elasticsearch Suggester API(自动补全)相关的知识,希望对你有一定的参考价值。

1.概念

1.1 补全api主要分为四类

  1. Term Suggester(纠错补全,输入错误的情况下补全正确的单词)
  2. Phrase Suggester(自动补全短语,输入一个单词补全整个短语)
  3. Completion Suggester(完成补全单词,输出如前半部分,补全整个单词)
  4. Context Suggester(上下文补全)

整体效果类似百度搜索,如图:

2. Completion suggester   自动补全 

针对自动补全场景而设计的建议器。此场景下用户每输入一个字符的时候,就需要即时发送一次查询请求到后端查找匹配项,在用户输入速度较高的情况下对后端响应速度要求比较苛刻。因此实现上它和前面两个Suggester采用了不同的数据结构,索引并非通过倒排来完成,而是将analyze过的数据编码成FST和索引一起存放。对于一个open状态的索引,FST会被ES整个装载到内存里的,进行前缀查找速度极快。但是FST只能用于前缀查找,这也是Completion Suggester的局限所在

2.1建立索引

put  /book


    "mappings": 
        "music" : 
            "properties" : 
                "suggest" : 
                    "type" : "completion"
                ,
                "title" : 
                    "type": "keyword"
                
            
        
    


注意,最终写入数据需要 /book/music

2.2插入数据

put /book/music/_doc/1

    "suggest":"爱一个人好难"


put /book/music/_doc/2

    "suggest":"爱一个人好难"


put /book/music/_doc/3

    "suggest":"爱真的需要勇气"

2.3自动补全查询 

示例1:查询建议根据前缀查询

POST book/music/_search

    "suggest": 
        "song-suggest" : 
            "prefix" : "爱",
            "completion" : 
                "field" : "suggest"
            
        
    

 示例1:查询建议根据前缀查询结果



    "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": "爱真的需要勇气"
                        
                    
                ]
            
        ]
    

示例2:对建议查询结果去重 


    "suggest": 
        "song-suggest" : 
            "prefix" : "爱",
            "completion" : 
                "field" : "suggest" ,
                 "skip_duplicates": true
            
        
    

本文部分参考:《ES系列十三、Elasticsearch Suggester API(自动补全)》 里面有整个四种补全的方式。

以上是关于ES系列Elasticsearch Suggester API(自动补全)的主要内容,如果未能解决你的问题,请参考以下文章

ElasticSearch 实现分词全文检索

ES实战系列-Elasticsearch安装

ElasticSearch系列二 ES基本使用及文档

ES系列6-ElasticSearch集群进阶

elasticsearch系列一:elasticsearch(ES简介安装&配置集成Ikanalyzer)

elasticsearch系列一:elasticsearch(ES简介安装&配置集成Ikanalyzer)