從最簡單的開始,了解索引(indexing)、搜索(search)以及聚合(aggregations)。
工具: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" } } } } } } } }