美文网首页
Elasticsearch学习笔记5: suggest实现搜索补

Elasticsearch学习笔记5: suggest实现搜索补

作者: MapleWhisper | 来源:发表于2019-08-09 14:27 被阅读0次

    原文链接 https://my.oschina.net/u/2299936/blog/1800139

    搜索补全功能使用的是es的completion suggester实现 官方文档地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

    先设置mapping:

    POST  /index/completion/_mapping

    {"properties":{"title": {"type":"text","analyzer":"ik_sync_smart"},"title_suggest": {"type":"completion","analyzer":"ik_sync_smart","search_analyzer":"ik_sync_smart"}  }}

    重点是title_suggest,这个字段就是之后我们搜索补全的字段,需要设置type为completion,analyzer按情况设置分析器

    索引数据:

    POST /index/completion/_bulk{"index": { } }{"title":"背景天安门广场大学","title_suggest":"背景天安门广场大学"}{"index": { } }{"title":"北京天安门","title_suggest":"北京天安门"}{"index": { } }{"title":"北京鸟巢","title_suggest":"北京鸟巢"}{"index": { } }{"title":"奥林匹克公园","title_suggest":"奥林匹克公园"}{"index": { } }{"title":"奥林匹克森林公园","title_suggest":"奥林匹克森林公园"}{"index": { } }{"title":"北京奥林匹克公园","title_suggest":"北京奥林匹克公园"}{"index": { } }{"title":"北京奥林匹克公园","title_suggest": {"input":"我爱中国","weight":100}}

    索引的时候可以对suggest字段,增加weight增加排序权重

    搜索补全:

    POST /index/completion/_search{"size": 0,"suggest": {"blog-suggest": {"prefix":"北京","completion": {"field":"title_suggest"}    }  }}返回:{"took": 27,"timed_out":false,"_shards": {"total": 3,"successful": 3,"skipped": 0,"failed": 0    },"hits": {"total": 0,"max_score": 0,"hits": []    },"suggest": {"blog-suggest": [            {"text":"北京","offset": 0,"length": 2,"options": [                    {"text":"北京天安门","_index":"jzbsearch","_type":"completion","_id":"AWK9FJDeGCbNhyb_l9I6","_score": 1,"_source": {"title":"北京天安门","title_suggest":"北京天安门"}                    },                    {"text":"北京奥林匹克公园","_index":"jzbsearch","_type":"completion","_id":"AWK9FJDeGCbNhyb_l9I-","_score": 1,"_source": {"title":"北京奥林匹克公园","title_suggest":"北京奥林匹克公园"}                    },                    {"text":"北京鸟巢","_index":"jzbsearch","_type":"completion","_id":"AWK9FJDeGCbNhyb_l9I7","_score": 1,"_source": {"title":"北京鸟巢","title_suggest":"北京鸟巢"}                    }                ]            }        ]    }}

    参考链接: https://elasticsearch.cn/article/142https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

    相关文章

      网友评论

          本文标题:Elasticsearch学习笔记5: suggest实现搜索补

          本文链接:https://www.haomeiwen.com/subject/raqhjctx.html