ES中文学习指南---入门篇
2016-01-12 13:08
387 查看
从最简单的开始,了解索引(indexing)、搜索(search)以及聚合(aggregations)。
工具:Sense插件,head插件
数据能够包含多个值的标签、数字和纯文本。
检索任何员工的所有信息。
支持结构化搜索,例如查找30岁以上的员工。
支持简单的全文搜索和更复杂的短语(phrase)搜索
高亮搜索结果中的关键字
能够利用图表管理分析这些数据
1.直接上传文档到ES,能自动创建索引,可以在elasticsearch.yml设置里增加
我们手动插入3条数据:
自动创建的索引结果为:
2. 进行简单的搜索:
返回结果:
3.复杂一些的搜索
工具:Sense插件,head插件
让我们建立一个员工目录
假设我们刚好在Megacorp工作,这时人力资源部门出于某种目的需要让我们创建一个员工目录,这个目录用于促进人文关怀和用于实时协同工作,所以它有以下不同的需求:数据能够包含多个值的标签、数字和纯文本。
检索任何员工的所有信息。
支持结构化搜索,例如查找30岁以上的员工。
支持简单的全文搜索和更复杂的短语(phrase)搜索
高亮搜索结果中的关键字
能够利用图表管理分析这些数据
1.直接上传文档到ES,能自动创建索引,可以在elasticsearch.yml设置里增加
action.auto_create_index: false来取消自动创建索引的功能。
我们手动插入3条数据:
PUT /megacorp/employee/1 { "first_name" : "John", "last_name" : "Smith", "age" : 25, "about" : "I love to go rock climbing", "interests": [ "sports", "music" ] } PUT /megacorp/employee/2 { "first_name" : "Jane", "last_name" : "Smith", "age" : 32, "about" : "I like to collect rock albums", "interests": [ "music" ] } PUT /megacorp/employee/3 { "first_name" : "Douglas", "last_name" : "Fir", "age" : 35, "about": "I like to build cabinets", "interests": [ "forestry" ] }
自动创建的索引结果为:
{ "state": "open", "settings": { "index": { "creation_date": "1452563538027", "number_of_shards": "5", "number_of_replicas": "1", "uuid": "Vf1xw2nxRi20wsuByM5Yvw", "version": { "created": "2010199" } } }, "mappings": { "employee": { "properties": { "about": { "type": "string" }, "last_name": { "type": "string" }, "interests": { "type": "string" }, "first_name": { "type": "string" }, "age": { "type": "long" } } } }, "aliases": [ ] }
2. 进行简单的搜索:
GET /megacorp/employee/_search?q=last_name:Smith
返回结果:
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.30685282, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.30685282, "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.30685282, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } } ] } }
3.复杂一些的搜索
GET /megacorp/employee/_search { "query" : { "match" : { "last_name" : "Smith" } } } //查找姓smith的人,确切值查询。 GET /megacorp/employee/_search { "query" : { "filtered" : { "filter" : { "range" : { "age" : { "gt" : 30 } } }, "query" : { "match" : { "last_name" : "smith" } } } } } //查找年龄大于30并且姓smith的人。 GET /megacorp/employee/_search { "query" : { "match" : { "about" : "rock climbing" } } } //全文检索,对about字段里的clock climbing搜索结果进行相关性排序。 返回如下:
{ "took": 5, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 2, "max_score": 0.16273327, "hits": [ { "_index": "megacorp", "_type": "employee", "_id": "1", "_score": 0.16273327, "_source": { "first_name": "John", "last_name": "Smith", "age": 25, "about": "I love to go rock climbing", "interests": [ "sports", "music" ] } }, { "_index": "megacorp", "_type": "employee", "_id": "2", "_score": 0.016878016, "_source": { "first_name": "Jane", "last_name": "Smith", "age": 32, "about": "I like to collect rock albums", "interests": [ "music" ] } } ]
//精确查找并将搜索结果高亮。↓ GET /megacorp/employee/_search { "query" : { "match_phrase" : { "about" : "rock climbing" } }, "highlight": { "fields" : { "about" : {} } } } //聚合查询↓ 数据量太大的时候会有性能的影响。 GET /megacorp/employee/_search { "aggs": { "all_interests": { "terms": { "field": "interests" } } } } GET /megacorp/employee/_search { "query": { "match": { "last_name": "smith" } }, "aggs": { "all_interests": { "terms": { "field": "interests" } } } } GET /megacorp/employee/_search { "aggs" : { "all_interests" : { "terms" : { "field" : "interests" }, "aggs" : { "avg_age" : { "avg" : { "field" : "age" } } } } } } } }
相关文章推荐
- C#中的Dictionary字典类介绍
- 流行的MySql版本
- jQuery全选选择器(基本上来自慕课网)
- 匈牙利命名表
- Google技术专家的建议:各种SdkVersion如何选择?
- PA-项目转资基础篇
- 仿QQ主界面Fragment简单应用
- 【15-16年年末复习】递归快速幂与非递归快速幂
- 局部敏感哈希-Locality Sensitive Hashing
- NYOJ1016(德莱联盟)(判断线段相交)
- 'Building workspace' has encountered a problem.Errors occurred during the build.
- Quartz定时任务简单实现
- 局部敏感哈希(Locality-Sensitive Hashing, LSH)方法介绍
- 微信、qq信息汇总、回复(一)
- 关闭 mongodb 出错: Error: shutdown failed: not authorized
- mongodb 的 C++驱动安装过程以及注意问题
- mongodb C/C++ driver 通过帐号验证登录mongo服务器并进行相应操作
- mongodb 建库过程(ubuntu shell 版)
- 开启MongoDB不能访问web控制台问题;; access MongoDB over HTTP
- python模拟登录微博网站利用urllib2 和 cookie