- 我們通常用用
_cat
API檢測集群是否健康。 確保9200端口號可用:
curl 'localhost:9200/_cat/health?v'
綠色表示一切正常, 黃色表示所有的數據可用但是部分副本還沒有分配,紅色表示部分數據因為某些原因不可用.
2.通過如下語句,我們可以獲取集群的節點列表:
curl 'localhost:9200/_cat/nodes?v'
3。通過如下語句,列出所有索引:
curl 'localhost:9200/_cat/indices?v'
返回結果:
4.創建索引
現在我們創建一個名為“customer”的索引,然后再查看所有的索引:
curl -XPUT 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
結果如下:
上圖中紅框所表示的是:我們有一個叫customer的索引,它有五個私有的分片以及一個副本,在它里面有0個文檔。
5.插入和獲取
現在我么插入一些數據到集群索引。我們必須給ES指定所以的類型。如下語句:"external" type, ID:1:
主體為JSON格式的語句: { "name": "John Doe" }
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
返回結果為:create:true 表示插入成功。
獲取GET,語句如下:
curl -XGET 'localhost:9200/customer/external/1?pretty'
其中含義為:獲取customer索引下類型為external,id為1的數據,pretty參數表示返回結果格式美觀。
6.刪除索引 DELETE
curl -XDELETE 'localhost:9200/customer?pretty'
curl 'localhost:9200/_cat/indices?v'
表示索引刪除成功。
7.通過以上命令語句的學習,我們發現索引的增刪改查有一個類似的格式,總結如下:
curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>
<REST Verb>:REST風格的語法謂詞
<Node>:節點ip
<port>:節點端口號,默認9200
<Index>:索引名
<Type>:索引類型
<ID>:操作對象的ID號
8 修改數據
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "John Doe"
}'
curl -XPUT 'localhost:9200/customer/external/1?pretty' -d '
{
"name": "Jane Doe"
}'
上述命令語句是:先新增id為1,name為John Doe的數據,然后將id為1的name修改為Jane Doe。
9.更新數據
9.1 這個例子展示如何將id為1文檔的name字段更新為Jane Doe:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe" }
}'
9.2 這個例子展示如何將id為1數據的name字段更新為Jane Doe同時增加字段age為20:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"doc": { "name": "Jane Doe", "age": 20 }
}'
9.3 也可以通過一些簡單的scripts來執行更新。一下語句通過使用script將年齡增加5:
curl -XPOST 'localhost:9200/customer/external/1/_update?pretty' -d '
{
"script" : "ctx._source.age += 5"
}'
10 刪除數據
刪除數據那是相當的直接. 下面的語句將執行刪除Customer中ID為2的數據:
curl -XDELETE 'localhost:9200/customer/external/2?pretty'
11 批處理
舉例:
下面語句將在一個批量操作中執行創建索引:
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
下面語句批處理執行更新id為1的數據然后執行刪除id為2的數據
curl -XPOST 'localhost:9200/customer/external/_bulk?pretty' -d '
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
12.導入數據集
你可以點擊這里下載示例數據集:accounts.json
其中每個數據都是如下格式:
{
"index":{"_id":"1"}
}
{
"account_number": 0,
"balance": 16623,
"firstname": "Bradshaw",
"lastname": "Mckenzie",
"age": 29,
"gender": "F",
"address": "244 Columbus Place",
"employer": "Euron",
"email": "bradshawmckenzie@euron.com",
"city": "Hobucken",
"state": "CO"
}
導入示例數據集:
curl -XPOST 'localhost:9200/bank/account/_bulk?pretty' --data-binary "@accounts.json"
curl 'localhost:9200/_cat/indices?v'
上圖紅框表示我們已經成功批量導入1000條數據索引到bank索引中。
13.查詢
Sample:
curl 'localhost:9200/bank/_search?q=*&pretty'
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : 1.0,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"_score" : 1.0, "_source" : {"account_number":1,"balance":39225,"firstname":"Amber","lastname":"Duke","age":32,"gender":"M","address":"880 Holmes Lane","employer":"Pyrami","email":"amberduke@pyrami.com","city":"Brogan","state":"IL"}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "6",
"_score" : 1.0, "_source" : {"account_number":6,"balance":5686,"firstname":"Hattie","lastname":"Bond","age":36,"gender":"M","address":"671 Bristol Street","employer":"Netagy","email":"hattiebond@netagy.com","city":"Dante","state":"TN"}
}, {
"_index" : "bank",
"_type" : "account",
上面示例返回所有bank中的索引數據。其中 q=* 表示匹配索引中所有的數據。
等價於:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} }
}'
14 查詢語言
匹配所有數據,但只返回1個:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"size": 1
}'
注意:如果siez不指定,則默認返回10條數據。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"from": 10,
"size": 10
}'
返回從11到20的數據。(索引下標從0開始)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"sort": { "balance": { "order": "desc" } }
}'
上述示例匹配所有的索引中的數據,按照balance字段降序排序,並且返回前10條(如果不指定size,默認最多返回10條)。
15.執行搜索
下面例子展示如何返回兩個字段(account_number balance)
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_all": {} },
"_source": ["account_number", "balance"]
}'
返回account_number 為20 的數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "account_number": 20 } }
}'
返回address中包含mill的所有數據::
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill" } }
}'
返回地址中包含mill或者lane的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match": { "address": "mill lane" } }
}'
和上面匹配單個詞語不同,下面這個例子是多匹配(match_phrase短語匹配),返回地址中包含短語 “mill lane”的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": { "match_phrase": { "address": "mill lane" } }
}'
以下是布爾查詢,布爾查詢允許我們將多個簡單的查詢組合成一個更復雜的布爾邏輯查詢。
這個例子將兩個查詢組合,返回地址中含有mill和lane的所有記錄數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
上述例子中,must表示所有查詢必須都為真才被認為匹配。
相反, 這個例子組合兩個查詢,返回地址中含有mill或者lane的所有記錄數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"should": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
上述例子中,bool表示查詢列表中只要有任何一個為真則認為匹配。
下面例子組合兩個查詢,返回地址中既沒有mill也沒有lane的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must_not": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}'
上述例子中,must_not表示查詢列表中沒有為真的(也就是全為假)時則認為匹配。
我們可以組合must、should、must_not來實現更加復雜的多級邏輯查詢。
下面這個例子返回年齡大於40歲、不居住在ID的所有數據:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "age": "40" } }
],
"must_not": [
{ "match": { "state": "ID" } }
]
}
}
}'
16.過濾filter(查詢條件設置)
下面這個例子使用了布爾查詢返回balance在20000到30000之間的所有數據。
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"query": {
"bool": {
"must": { "match_all": {} },
"filter": {
"range": {
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}'
17 聚合 Aggregations
下面這個例子: 將所有的數據按照state分組(group),然后按照分組記錄數從大到小排序,返回前十條(默認):
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
}
}
}
}'
注意:我們設置size=0,不顯示查詢hits,因為我們只想看返回的聚合結果。
上述語句類似於以下SQL語句:
SELECT state, COUNT(*) FROM bank GROUP BY state ORDER BY COUNT(*) DESC
下面這個實例按照state分組,降序排序,返回balance的平均值:
curl -XPOST 'localhost:9200/bank/_search?pretty' -d '
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}'