- 必記知識點
Elasticsearch可以接近實時的搜索和存儲大量數據。Elasticsearch是一個近實時的搜索平台。這意味着當你導入一個文檔並把它變成可搜索的時間僅會有輕微的延時。
文檔是基本存儲單元,以json格式存儲,比如說一個用戶信息,類似數據庫里的單條數據。索引是多個同一類文檔的集合,類似數據庫里的表。Elasticsearch支持集群部署,一台服務器是一個節點,多個節點組成一個集群,每個節點有唯一節點名,同一集群里的節點有共同的集群名稱。索引可以分片和復制,類似數據庫的水平分片和主從復制,主索引分片和復制分片需要在不同的節點,來保證高可用。
- API
Elasticsearch的api為rest風格,可以通過url增刪改查,格式為:<REST Verb> /<Index>/<Type>/<ID>
REST Verb為:PUT、DELETE、GET、POST
Index為索引
Type為索引里的文檔,如doc
ID為文檔的id,可以在創建文檔時定義,和數據庫表的id類似,當將文檔加入索引時,ID部分並不是必須的。如果沒有指定,Elasticsearch將會生產一個隨機的ID,然后使用它去索引文檔。Elasticsearch生成的ID將會在API調用成功后返回。
PUT:
示例: PUT /customer/doc/1?pretty 請求將會將一個ID為1的文檔加入customer索引。curl命令:curl -XPUT 'localhost:9200/customer/doc/1?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "John Doe" } '
如果我們再次執行上面的請求,Elasticsearch將會根據相同的ID更新之前的文檔。
DELETE:
刪除ID為2的文檔: curl -XDELETE 'localhost:9200/customer/doc/2?pretty&pretty'
POST:
當我們沒有明確指定ID的時候,我們需要使用POST
方法代替PUT
來發送請求。curl -XPOST 'localhost:9200/customer/doc?pretty&pretty' -H 'Content-Type: application/json' -d' { "name": "Jane Doe" } '
如下的例子演示如何去更新我們的之前ID為1的文檔,
curl -XPOST 'localhost:9200/customer/doc/1/_update?pretty&pretty' -H 'Content-Type: application/json' -d' { "doc": { "name": "Jane Doe" } } '
GET:
獲取ID為2的文檔: curl -XGET 'localhost:9200/customer/doc/2?pretty&pretty'
批處理_bulk
在一個批操作中創建了兩個文檔:
curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"index":{"_id":"1"}} {"name": "John Doe" } {"index":{"_id":"2"}} {"name": "Jane Doe" } '
先更新ID為1的文檔,然后刪除ID為2的文檔:
curl -XPOST 'localhost:9200/customer/doc/_bulk?pretty&pretty' -H 'Content-Type: application/json' -d' {"update":{"_id":"1"}} {"doc": { "name": "John Doe becomes Jane Doe" } } {"delete":{"_id":"2"}} '
查詢語言
執行搜索有兩種基礎的方式,一種是在請求的URL中加入參數來實現,另一種方式是將請求內容放到請求體中。使用請求體可以讓你的JSON數據以一種更加可讀和更加富有展現力的方式發送。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_all": {} } } '
query
部分告訴我們我們的查詢定義是什么,match_all
部分簡單指定了我們想去執行的查詢類型,意思就是在索引中搜索所有的文檔。
除了query
參數,我們還可以通過其他的參數影響搜索結果。 sort
來指定搜索結果的順序,from
參數(從0開始)指定了從哪個文檔索引開始,size
參數指定了從from
指定的索引開始返回多少個文檔,用來分頁。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_all": {} }, "_source": ["account_number", "balance"] } '
_source指定需要返回的屬性名。示例演示了如何返回兩個屬性,account_number
和 balance
(在_source
中):
match
,它可以被認為是基本的屬性搜索查詢(就是通過特定的一個或多個屬性來搜索)。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match": { "account_number": 20 } } } '
match_phrase
,將返回所有address中包含“mill lane”的文檔。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "match_phrase": { "address": "mill lane" } } } '
bool
查詢允許我們使用布爾邏輯將小的查詢組成大的查詢。bool
must
子句指定了所有匹配文檔必須滿足的條件。
GET /bank/_search { "query": { "bool": { "must": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } }
如下示例組合兩個match
查詢並且返回所有address屬性中包含 “mill” 或 “lane” 的賬戶文檔,bool
should
子句指定了匹配文檔只要滿足其中的任何一個條件即可匹配。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "bool": { "should": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } '
如下示例組合兩個match
查詢並且返回所有address屬性中既不包含 “mill” 也不包含 “lane” 的賬戶文檔,bool
must_not
子句指定了其中的任何一個條件都不滿足時即可匹配。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "bool": { "must_not": [ { "match": { "address": "mill" } }, { "match": { "address": "lane" } } ] } } } '
我們可以在一個bool
查詢中同時指定must
,should
和must_not
子句。此外,我們也可以在一個bool
子句中組合另一個bool
來模擬任何復雜的多重布爾邏輯。
bool
查詢也支持 filter
子句,它允許我們可以在不改變得分計算邏輯的的情況下限制其他子句匹配的查詢結果。為了示例說明,讓我們介紹一下range
查詢,它允許我們通過一個值區間來過濾文檔。這個通常用在數值和日期過濾上。
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "query": { "bool": { "must": { "match_all": {} }, "filter": { "range": { "balance": { "gte": 20000, "lte": 30000 } } } } } } '
執行聚合
類似SQL的聚合函數,如下的例子將賬戶按state進行分組,然后按count降序(默認)返回前10組(默認)states,計算每個state分組的平均賬戶余額
curl -XGET 'localhost:9200/bank/_search?pretty' -H 'Content-Type: application/json' -d' { "size": 0, "aggs": { "group_by_state": { "terms": { "field": "state.keyword" }, "aggs": { "average_balance": { "avg": { "field": "balance" } } } } } } '
還有很多其它的聚合功能在這里我們就不去詳細介紹了。如果你想了解更多,可以參考https://blog.csdn.net/hololens/article/details/78932628