从最简单的开始,了解索引(indexing)、搜索(search)以及聚合(aggregations)。
工具:Sense插件,head插件
假设我们刚好在Megacorp工作,这时人力资源部门出于某种目的需要让我们创建一个员工目录,这个目录用于促进人文关怀和用于实时协同工作,所以它有以下不同的需求:
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" } } } } } } } }
原文:http://www.cnblogs.com/flyingbee6/p/5123460.html