3-Elasticsearch深入搜索-Suggester

一起来玩Elasticsearch,加我微信:wx1250134974

Elasticsearch认证复习准备

https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html

##suggest应用场景:

现代的搜索引擎,一般会具备"Suggest As You Type"功能,即在用户输入搜索的过程中,进行自动补全或者纠错。 通过协助用户输入更精准的关键词,提高后续全文搜索阶段文档匹配的程度。例如在Google上输入部分关键词,甚至输入拼写错误的关键词时,它依然能够提示出用户想要输入的内容,根据使用场景的不同,Elasticsearch里设计了4种类别的Suggester,分别是:Term Suggester

、Phrase Suggester、Completion Suggester、Context Suggester

 

  1. Term Suggester

原理:用户输入的text将会先被分析器分析成单个term,然后每个term都将作为生成建议terms的基础。每个term按照编辑距离(更改几次和建议term匹配)进行生成建议terms。这种方式召回率较高,准确性相对较低。

PUT /blogs2/  #索引建立

{

  "mappings": {

    "tech": {

      "properties": {

        "body": {

          "type": "text"

        }

      }

    }

  }

}

 

POST _bulk/?refresh=true  #索引数据

{ "index" : { "_index" : "blogs2", "_type" : "tech" } }

{ "body": "Lucene is cool"}

{ "index" : { "_index" : "blogs2", "_type" : "tech" } }

{ "body": "Elasticsearch builds on top of lucene"}

 

 

 

POST /blogs2/_search   #Term Suggester查询

{

  "suggest": {

    "my-suggestion": {

      "text": "lucne rock",

      "term": {

        "field": "body"

      }

    }

  }

}

注:my-suggestion建议查询的名字,text用户的输入内容,term指的就是Term Suggester查询,field针对的字段

 

 

#其它Term Suggester查询实例

POST twitter/_search  #和query组合使用

{

  "query" : {

    "match": {

      "message": "tring out Elasticsearch"

    }

  },

  "suggest" : {

    "my-suggestion" : {

      "text" : "trying out Elasticsearch",

      "term" : {

        "field" : "message"

      }

    }

  }

}

 

POST _search  #多个suggestions定义

{

  "suggest": {

    "my-suggest-1" : {

      "text" : "tring out Elasticsearch",

      "term" : {

        "field" : "message"

      }

    },

    "my-suggest-2" : {

      "text" : "kmichy",

      "term" : {

        "field" : "user"

      }

    }

  }

}

 

 

 

POST _search  #全局定义text,可以用下方suggestions的text覆盖掉

 

{

  "suggest": {

    "text" : "tring out Elasticsearch",

    "my-suggest-1" : {

      "term" : {

        "field" : "message"

      }

    },

    "my-suggest-2" : {

       "term" : {

        "field" : "user"

       }

    }

  }

}

 

附加:Term Suggester其它选项参见下方链接(可参看《深入理解Elasticsearch》7.1章节)

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-term.html

 

 

 

  1. Phrase Suggester

原理:在term基础上增加了短语计算逻辑,返回的建议项是短语而不是单个term。召回率降低,相对准确度上升

POST /blogs2/_search

{

  "suggest": {

    "my-suggestion": {

      "text": "lucne and elasticsear rock",

      "phrase": {

        "field": "body",

        "highlight": {

          "pre_tag": "<em>",

          "post_tag": "</em>"

        }

      }

    }

  }

}

注:和Term Suggester的查询很像,返回也很像,只不过Phrase Suggester返回的是短语, Term Suggester返回的是单个词

 

 

附加:Phrase Suggester其它参数参见链接,可参看《深入理解Elasticsearch》7.1章节的中文翻译

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-phrase.html

 

 

 

  1. Completion Suggester

原理:将用户的输入作为搜索建议的前缀。召回率低,精确度相对最高

PUT music        #设置mapping

{

    "mappings": {

        "song" : {

            "properties" : {

                "suggest" : {

                    "type" : "completion"  #必须使用这种类型

                },

                "title" : {

                    "type": "keyword"

                }

            }

        }

    }

}

 

PUT music/song/1?refresh   #索引数据

{

    "suggest" : {

        "input": [ "Nevermind", "Nirvana" ],   #索引的文本

        "weight" : 34                          #term权重,影响打分

    }

}

 

PUT music/song/2?refresh  #索引数据

{

  "suggest":{

    "input":"block",

    "weight": 3

  }

}

 

PUT music/song/3?refresh #索引数据

{

  "suggest":{

    "input":"back"

  }

}

 

POST music/_search?pretty  #查询

{

    "suggest": {

        "song-suggest" : {

            "prefix" : "ba",

            "completion" : {

                "field" : "suggest"

            }

        }

    }

}

POST music/_search

{

    "_source": "",   #过滤掉文档只要建议

    "suggest": {

        "song-suggest" : {

            "prefix" : "nir",

            "completion" : {

                "field" : "suggest",

                "size" : 5     #返回的建议数量

            }

        }

    }

}

 

附加:Completion Suggester其它参数参见链接,可参看《深入理解Elasticsearch》7.1章节的中文翻译

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-suggesters-completion.html

 

  1. Context Suggester

原理:在Completion Suggester基础上添加了上下文过滤建议功能,搜索时提供上下文,可以更精确的返回相关的建议。召回率更低,准确性更高

PUT place_path_category   #索引映射

{

    "mappings": {

        "shops" : {

            "properties" : {

                "suggest" : {

                    "type" : "completion",

                    "contexts": [

                        {

                            "name": "place_type", #第一个上下文名字

                            "type": "category",

                            "path": "cat"    #使用cat路径

                        },

                        {

                            "name": "location", #第二个上下文名字

                            "type": "geo",

                            "precision": 4,

                            "path": "loc"   #使用loc路径

                        }

                    ]

                },

                "loc": {

                    "type": "geo_point"   #定义了loc路径的类型

                }

            }

        }

    }

}

 

PUT place_path_category/shops/1   #索引数据

{

    "suggest": {

        "input": ["timmy's", "starbucks", "dunkin donuts"],

        "contexts": {

            "place_type": ["cafe", "food"]  #没定义路径必须这样传

        }

    }

}

 

PUT place_path_category/shops/2 #索引数据

{

    "suggest": ["timmy's", "starbucks", "dunkin donuts"],

    "cat": ["cafe", "food"]

}

 

POST place_path_category/_search?pretty  #查询

{

    "suggest": {

        "place_suggestion" : {

            "prefix" : "tim",

            "completion" : {

                "field" : "suggest",

                "size": 10,

                "contexts": {

                    "place_type": [ "cafe", "restaurants" ]

                }

            }

        }

    }

}

POST place_path_category/_search?pretty #查询

{

    "suggest": {

        "place_suggestion" : {

            "prefix" : "tim",

            "completion" : {

                "field" : "suggest",

                "size": 10,

                "contexts": {

                    "place_type": [

                        { "context" : "cafe" },

                        { "context" : "restaurants", "boost": 2 } #提供权重

                     ]

                }

            }

        }

    }

}

 

 

PUT place_path_category/shops/1  #索引地理位置上下文

{

  "suggest": [

    "timmy's",

    "starbucks",

    "dunkin donuts"

  ],

  "loc": [

    {

      "lat": 43.6624803,

      "lon": -79.3863353

    },

    {

      "lat": 43.6624718,

      "lon": -79.3873327

    }

  ]

}

 

POST place_path_category/_search #查询

{

    "suggest": {

        "place_suggestion" : {

            "prefix" : "tim",

            "completion" : {

                "field" : "suggest",

                "size": 10,

                "contexts": {

                    "location": {

                        "lat": 43.662,

                        "lon": -79.380

                    }

                }

            }

        }

    }

}

 

POST place_path_category/_search?pretty  #加权重

{

    "suggest": {

        "place_suggestion" : {

            "prefix" : "tim",

            "completion" : {

                "field" : "suggest",

                "size": 10,

                "contexts": {

                    "location": [

                        {

                            "lat": 43.6624803,

                            "lon": -79.3863353,

                            "precision": 2

                        },

                        {

                            "context": {

                                "lat": 43.6624803,

                                "lon": -79.3863353

                            },

                            "boost": 2

                        }

                     ]

                }

            }

        }

    }

}

 

附加:其它参数参看

https://www.elastic.co/guide/en/elasticsearch/reference/5.6/suggester-context.html

 

 

参看过的连接:

https://elasticsearch.cn/article/142

https://www.cnblogs.com/Neeo/articles/10695019.html

https://www.cnblogs.com/Neeo/articles/10695031.html

 

一起来玩Elasticsearch,加我微信:wx1250134974

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值