您的位置:首页 > 编程语言 > Python开发

ElasticSearch 自动补全实现

2016-09-26 10:11 225 查看

介绍

elasticsearch的suggester共有四类(term suggester, phrase suggester, completion suggester, context suggester), 其中completion suggester作为搜索框中的自动补齐功能,尤为常用。实现一个完整的completion suggester功能,需要三个步骤:创建映射,插入索引数据,搜索数据。

此外,本文此基础上用python实现ES自动补全。希望给大家一点启示吧!

说明

相关说明和参考只看官方文档即可:

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#completion-suggester-mapping

关于创建映射时的参数说明:



查询补全时参数说明:



先创建索引、创建映射、插入数据

========================创建映射=========================
curl -XPUT localhost:9200/index
curl -XPUT localhost:9200/index/test/_mapping -d '{
"test" : {
"properties" : {
"name" : { "type" : "string" },
"name_suggest" : { "type" : "completion",
"analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
},
"tag_suggest" : { "type" : "completion",
"analyzer" : "simple",
"search_analyzer" : "simple",
"payloads" : true
}
}
}
}'
========================插入索引数据=========================
curl -XPUT 'localhost:9200/index/test/1?refresh=true' -d '{
"name" : "xdy",
"name_suggest" : {
"input": ["xdy", "hdu"]
}
}'

curl -XPUT 'localhost:9200/index/test/2?refresh=true' -d '{
"name" : "lz",
"name_suggest" : {
"input": ["lz", "hdu"],
"output": "lz-hdu"
}
}'

curl -XPUT 'localhost:9200/index/test/3?refresh=true' -d '{
"name" : "xck",
"name_suggest" : {
"input": ["xck", "gbd"]
}
}'

curl -XPUT 'localhost:9200/index/test/4?refresh=true' -d '{
"name_suggest" : {
"input": [ "hz", "bdata", "cas"]
}
}'

curl -XPUT 'localhost:9200/index/test/5?refresh=true' -d '{
"tag_suggest" : {
"input": ["bfd", "bdata", "hadoop"]
}
}'
========================创建查询=========================
curl -XPOST 'localhost:9200/index/_suggest?pretty' -d '{
"index-suggest" : {
"text" : "b",
"completion" : {
"field" : "name_suggest"
}
}
}'


Python 接口实现自动补全

from elasticsearch import Elasticsearch

def suggest(tag, query, suggest_size=10):
# 设置条件
es_suggest_options = set_suggest_optional(query, tag, suggest_size)
# 发起检索。
es_client = Elasticsearch(hosts=["127.0.0.1:9200"], timeout=5000)
es_result = es_client.suggest(index='index',body=es_suggest_options)
# 得到结果。
final_results = get_suggest_list(es_result)
return final_results

def get_suggest_list(es_result):
result_items = es_result['suggest'][0]["options"]
final_results = []
for item in result_items:
final_results.append(item['text'])
return final_results

def set_suggest_optional(query, tag, suggest_size):
# 检索选项
es_suggest_options = {
"suggest": {
"text": query,
"completion": {
"field": tag,
"size": suggest_size
}
}
}
return es_suggest_options

if __name__ == '__main__':
final_results = suggest('name_suggest', 'b', 2)
for item in final_results:
print item
print '========================================='
final_results = suggest('tag_suggest', 'b', 2)
for item in final_results:
print item


输出:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息