Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4447036
  • 博文数量: 1214
  • 博客积分: 13195
  • 博客等级: 上将
  • 技术积分: 9105
  • 用 户 组: 普通用户
  • 注册时间: 2007-01-19 14:41
个人简介

C++,python,热爱算法和机器学习

文章分类

全部博文(1214)

文章存档

2021年(13)

2020年(49)

2019年(14)

2018年(27)

2017年(69)

2016年(100)

2015年(106)

2014年(240)

2013年(5)

2012年(193)

2011年(155)

2010年(93)

2009年(62)

2008年(51)

2007年(37)

分类: 服务器与存储

2016-11-03 15:18:41

原文地址:    http://blog.csdn.net/xsdxs/article/details/52667794

介绍

elasticsearch的suggester共有四类(term suggester, phrase suggester, completion suggester, context suggester), 其中completion suggester作为搜索框中的自动补齐功能,尤为常用。实现一个完整的completion suggester功能,需要三个步骤:创建映射,插入索引数据,搜索数据。 
此外,本文此基础上用实现ES自动补全。希望给大家一点启示吧!

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

========================创建映射=========================
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"
        }
    }
}'
		
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

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
		
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40

输出: 
这里写图片描述

说明和参考

相关说明和参考只看官方文档即可: 
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-completion.html#completion-suggester-mapping

关于创建映射时的参数说明: 
这里写图片描述

查询补全时参数说明: 
这里写图片描述

阅读(4304) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~