document數據格式node
public class Employee { private String email; private String firstName; private String lastName; private EmployeeInfo info; private Date joinDate; } private class EmployeeInfo { private String bio; // 性格 private Integer age; private String[] interests; // 興趣愛好 } EmployeeInfo info = new EmployeeInfo(); info.setBio("curious and modest"); info.setAge(30); info.setInterests(new String[]{"bike", "climb"}); Employee employee = new Employee(); employee.setEmail("zhangsan@sina.com"); employee.setFirstName("san"); employee.setLastName("zhang"); employee.setInfo(info); employee.setJoinDate(new Date());
employee id email first_name last_name join_date 001 hangsan@sina.com san zhang 2017/01/01 employee_info employee_id bio age interests 001 curious and modest 30 bike, climb
{ "email": "zhangsan@sina.com", "first_name": "san", "last_name": "zhang", "info": { "bio": "curious and modest", "age": 30, "interests": [ "bike", "climb" ] }, "join_date": "2017/01/01" }
GET /_cat/health?v
green:每一個索引的primary shard和replica shard都是active狀態的
yellow:每一個索引的primary shard都是active狀態的,可是部分replica shard不是active狀態,處於不可用的狀態
red:不是全部索引的primary shard都是active狀態的,部分索引有數據丟失了python
如今只啓動動了一個es進程,至關於就只有一個node。如今es中有一個index,就是kibana本身內置創建的index。因爲默認的配置是給每一個index分配5個primary shard和5個replica shard,並且primary shard和replica shard不能在同一臺機器上(爲了容錯)。如今kibana本身創建的index是1個primary shard和1個replica shard。當前就一個node,因此只有1個primary shard被分配了和啓動了,可是一個replica shard沒有第二臺機器去啓動。只要啓動第二個es進程,就會在es集羣中有2個node,而後那1個replica shard就會自動分配過去,而後cluster status就會變成green狀態。數據庫
#語法 PUT /index/type/id { "json數據" } # 添加商品1 PUT /ecommerce/product/1 { "name" : "gaolujie yagao", #商品名稱 "desc" : "gaoxiao meibai", #商品描述 "price" : 30, #商品價格 "producer" : "gaolujie producer", #生廠廠家 "tags": [ "meibai", "fangzhu" ] #產品標籤 } #添加商品2 PUT /ecommerce/product/2 { "name" : "jiajieshi yagao", "desc" : "youxiao fangzhu", "price" : 25, "producer" : "jiajieshi producer", "tags": [ "fangzhu" ] } #添加商品3 PUT /ecommerce/product/3 { "name" : "zhonghua yagao", "desc" : "caoben zhiwu", "price" : 40, "producer" : "zhonghua producer", "tags": [ "qingxin" ] }
es會自動創建index和type,不須要提早建立,並且es默認會對document每一個field都創建倒排索引,讓其能夠被搜索json
#語法 GET /index/type/id GET /ecommerce/product/1
{ "_index": "ecommerce", "_type": "product", "_id": "1", "_version": 1, "found": true, "_source": { "name": "gaolujie yagao", "desc": "gaoxiao meibai", "price": 30, "producer": "gaolujie producer", "tags": [ "meibai", "fangzhu" ] } }
PUT /ecommerce/product/1 { "name" : "jiaqiangban gaolujie yagao", "desc" : "gaoxiao meibai", "price" : 30, "producer" : "gaolujie producer", "tags": [ "meibai", "fangzhu" ] }
DELETE /ecommerce/product/1
query string search的由來:由於search參數都是以http請求的query string來附帶的數據結構
{ "took": 3, "timed_out": false, "_shards": { "total": 5, "successful": 5, "failed": 0 }, "hits": { "total": 3, "max_score": 1, "hits": ...... { "_index": "ecommerce", "_type": "product", "_id": "3", "_score": 1, "_source": { "name": "zhonghua yagao", "desc": "caoben zhiwu", "price": 40, "producer": "zhonghua producer", "tags": [ "qingxin" ] ...... }
took:耗費了幾毫秒
timed_out:是否超時,這裏是沒有
_shards:數據拆成了5個分片,因此對於搜索請求,會打到全部的primary shard(或者是它的某個replica shard)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高
hits.hits:包含了匹配搜索的document的詳細數據curl
按售價降序排列工具
GET /ecommerce/product/_search?q=name:yagao&sort=price:desc
適用場景url
適用於臨時的在命令行使用一些工具,好比curl,快速的發出請求,來檢索想要的信息;若是查詢請求很複雜,是很難去構建的在生產環境中,幾乎不多使用query string search命令行
DSL:Domain Specified Language,特定領域的語言
http request body:請求體,能夠用json的格式來構建查詢語法,比較方便,能夠構建各類複雜的語法,比query string search確定強大多了rest
查詢全部
GET /ecommerce/product/_search { "query": { "match_all": {} } }
條件查詢
查詢名稱包含yagao的商品,同時按照價格降序排序
GET /ecommerce/product/_search { "query" : { "match" : { "name" : "yagao" } }, "sort": [ { "price": "desc" } ] }
分頁查詢
GET /ecommerce/product/_search { "query": { "match_all": {} }, "from": 1, "size": 1 }
指定查詢
更加適合生產環境的使用,能夠構建複雜的查詢
GET /ecommerce/product/_search { "query": { "match_all": {} }, "_source": ["name", "price"] }
過濾查詢
搜索商品名稱包含yagao,並且售價大於25元的商品
GET /ecommerce/product/_search { "query" : { "bool" : { "must" : { "match" : { "name" : "yagao" } }, "filter" : { "range" : { "price" : { "gt" : 25 } } } } } }
GET /ecommerce/product/_search { "query" : { "match" : { "producer" : "yagao producer" } } }
producer這個字段,會先被拆解,創建倒排索引
special | 4 | |
---|---|---|
yagao | 4 | |
producer | 1,2,3,4 | |
gaolujie | 1 | |
zhognhua | 3 | |
jiajieshi | 2 |
yagao producer 會被拆解爲 yagao和producer
跟全文檢索相對應,相反,全文檢索會將輸入的搜索串拆解開來,去倒排索引裏面去一一匹配,只要能匹配上任意一個拆解後的單詞,就能夠做爲結果返回
phrase search,要求輸入的搜索串,必須在指定的字段文本中,徹底包含如出一轍的,才能夠算匹配,才能做爲結果返回
GET /ecommerce/product/_search { "query" : { "match_phrase" : { "producer" : "yagao producer" } } }
GET /ecommerce/product/_search { "query" : { "match" : { "producer" : "producer" } }, "highlight": { "fields" : { "producer" : {} } } }