1 es啟動時,命令行設(shè)置名字
./elasticsearch -Ecluster.name=my_cluster_name -Enode.name=my_node_name
2 REST API可以做什么
1.檢查集群,節(jié)點,索引的健康,狀態(tài),統(tǒng)計
2.管理集群,節(jié)點,索引的數(shù)據(jù)和元數(shù)據(jù)
3.執(zhí)行CRUD和搜索操作
4.執(zhí)行高級搜索操作,比如分頁,排序,過濾,腳本,聚合等
2.1 集群健康檢測api
http://192.168.0.128:9200/_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1498119164 16:12:44 es-cluster yellow 1 1 20 20 0 0 20 0 - 50.0%
說明:集群的狀態(tài)描述:
green:一切都準(zhǔn)備好了,集群功能全部可用;
yellow:數(shù)據(jù)準(zhǔn)備好了,但是副本還沒有分配好,集群功能全部可用;
red:有些數(shù)據(jù)不可用,但是集群部分功能可用;
2.2 集群節(jié)點列表api
http://192.168.0.128:9200/_cat/nodes?v
ip heap.percent ram.percent cpu load_1m load_5m load_15m node.role master name
192.168.0.128 19 72 58 mdi * master
2.3 列出集群中所有的索引
http://192.168.0.128:9200/_cat/indices?v
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size
yellow open ttl mPSsvTX3TbSsSQKUcJqtbA 5 1 0 0 795b 795b
yellow open java 3IfBdV_-T8SuvNSK72jBqQ 5 1 0 0 650b 650b
yellow open book -rZ8v4AfTDyPPTm3oZ_qLQ 5 1 16 0 64.7kb 64.7kb
yellow open index 4BAj2ycsSGyosLYPmTQEZw 5 1 0 0 795b 795b
上面health都為yellow是因為只有一個node,es默認(rèn)創(chuàng)建一個副本,等待其他的節(jié)點加入
2.4 創(chuàng)建一個customer的index
http://192.168.0.128:9200/customer?pretty
method: PUT
response:
{
"acknowledged": true,
"shards_acknowledged": true
}
2.5 創(chuàng)建文檔索引和查詢文檔
http://192.168.0.128:9200/customer/person/1?pretty
method: PUT
params: {"name":"張三","age":34,"sex":"男"}
response:
{
"_index": "customer",
"_type": "person",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
----------------------------------------------------------
http://192.168.0.128:9200/customer/person?pretty
method: POST
params: {"name":"張三","age":34,"sex":"男"}
response:
{
"_index": "customer",
"_type": "person",
"_id": "AVzTAOlNiSjTxTQlMxWw",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
-----------------------------------------------------------
http://192.168.0.128:9200/customer/person/AVzTAOlNiSjTxTQlMxWw/_update?pretty
method: POST
params:
{
"doc":{"name":"李四","age":44,"sex":"男"}
}
response:
{
"_index": "customer",
"_type": "person",
"_id": "AVzTAOlNiSjTxTQlMxWw",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
ps:AVzTAOlNiSjTxTQlMxWw是文檔id
---------------------------------------------------------------
使用腳本更新,ctx._source指向當(dāng)前source文檔
http://192.168.0.128:9200/customer/person/AVzTAOlNiSjTxTQlMxWw/_update?pretty
method: POST
params:
{
"script" : "ctx._source.age += 5"
}
response:
{
"_index": "customer",
"_type": "person",
"_id": "AVzTAOlNiSjTxTQlMxWw",
"_version": 3,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
-------------------------------------------------------
http://192.168.0.128:9200/customer/person/1?pretty
method: GET
response:
{
"_index": "customer",
"_type": "person",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "張三",
"age": 34,
"sex": "男"
}
}
2.6 刪除索引
http://192.168.0.128:9200/customer
method: DELETE
response:
{
"acknowledged": true
}
2.7 根據(jù)文檔id,刪除文檔
http://192.168.0.128:9200/customer/person/AVzTAOlNiSjTxTQlMxWw?pretty
method: DELETE
response:
{
"found": true,
"_index": "customer",
"_type": "person",
"_id": "AVzTAOlNiSjTxTQlMxWw",
"_version": 4,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
2.8 elasticsearch提供了批處理_bulk API,可以同時處理index,update,delete操作.批處理減少網(wǎng)絡(luò)請求.批處理時,如果某個動作失敗了,不會影響其他的動作;批處理返回結(jié)果按執(zhí)行的順序返回動作執(zhí)行狀態(tài),可以檢測是否失敗.
批量添加兩個index
http://192.168.0.128:9200/customer/person/_bulk?pretty
method: POST
params:
{"index" : {"_id" : 2}}
{"name" : "趙六","age" : 23}
{"index" : {"_id" : 3}}
{"name" : "王五","age" : 53}
response:
{
"took" : 1541,
"errors" : false,
"items" : [
{
"index" : {
"_index" : "customer",
"_type" : "person",
"_id" : "2",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
},
{
"index" : {
"_index" : "customer",
"_type" : "person",
"_id" : "3",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"created" : true,
"status" : 201
}
}
]
}
------------------------------------------------------
更新文檔2,刪除文檔3
http://192.168.0.128:9200/customer/person/_bulk?pretty
method: POST
params:
{"update" : {"_id" : 2}}
{"doc" : {"age" : 33}}
{"delete" : {"_id" : 3}}
response:
{
"took": 941,
"errors": false,
"items": [
{
"update": {
"_index": "customer",
"_type": "person",
"_id": "2",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
},
{
"delete": {
"found": true,
"_index": "customer",
"_type": "person",
"_id": "3",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"status": 200
}
}
]
}
2.9 使用REST API搜索文檔
搜索所有文檔,結(jié)果按account_number升序排序
http://192.168.0.128:9200/bank/_search?q=*&sort=account_number:asc&pretty
等價的寫法:
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
],
"from" : 5, //從第5條開始,默認(rèn)是0
"size" : 1 //返回1條,默認(rèn)是10條
}
response:
{
"took": 32, //搜索時間,單位:毫秒
"timed_out": false, //搜索是否超時
"_shards": { //搜索分片數(shù)量,以及成功和失敗的數(shù)量
"total": 5,
"successful": 5,
"failed": 0
},
"hits": { //搜索結(jié)果
"total": 1000, //滿足搜索條件的文檔數(shù)量
"max_score": null,
"hits": [ //真實搜索結(jié)果數(shù)組,默認(rèn)顯示10條
{
"_index": "bank",
"_type": "account",
"_id": "0",
"_score": null,
"_source": {
"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"
},
"sort": [ //排序的結(jié)果
0
]
}
]
}
}
------------------------------------------
搜索所有的文檔,返回前2條,并顯示指定的fields
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": { "match_all": {} },
"_source" : ["account_number","balance","email"], //返回指定的字段
"size" : 2
}
response:
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1000,
"max_score": 1,
"hits": [
{
"_index": "bank",
"_type": "account",
"_id": "25",
"_score": 1,
"_source": {
"account_number": 25,
"balance": 40540,
"email": "virginiaayala@filodyne.com"
}
},
{
"_index": "bank",
"_type": "account",
"_id": "44",
"_score": 1,
"_source": {
"account_number": 44,
"balance": 34487,
"email": "aureliaharding@orbalix.com"
}
}
]
}
}
------------------------------------
搜索account_number為20的文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": { "match": {"account_number" : 20} }
}
------------------------------
搜索address中含有mill的所有文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": { "match": {"address" : "mill"} }
}
---------------------------------
使用match_phrase匹配address中含有"mill lane"短語的文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": { "match_phrase": {"address" : "mill lane"} }
}
---------------------------------
使用bool query匹配address中同時含有"mill "和"lane"短語的文檔,must:and
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": {
"bool": {
"must": [
{ "match": { "address": "mill" } },
{ "match": { "address": "lane" } }
]
}
}
}
與之類似的:
should:or關(guān)系
must_not:即不含"mill",也不含"lane"的文檔
bool query可以同時包含must,should,must_not組成復(fù)雜的查詢
2.10 文檔score:根據(jù)搜索條件估算一個文檔匹配程度的相對的數(shù)值.得分越高,文檔越有價值;反之,價值越低.有些情況不需要score(比如"filter""),es會檢測自動優(yōu)化查詢,不計算得分.
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"query": {
"bool": {
"must": { "match_all": {} }, //查詢所有的文檔
"filter": { //過濾,不計算得分,從結(jié)果可以查出所有的score都為1,是個常量
"range": { //范圍查詢,適用于numeric或deta 類型
"balance": {
"gte": 20000,
"lte": 30000
}
}
}
}
}
}
2.11 執(zhí)行聚合:es提供了分組和統(tǒng)計的能力,這就是聚合.可以認(rèn)為就是sql中的group by和aggregate 功能.es在聚合時同時返回搜索的文檔和聚合兩部分.
按state分組聚合,不返回搜索的文檔
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"size": 0,//不返回搜索的文檔
"aggs": {//聚合
"group_by_state": {
"terms": {
"field": "state.keyword" //按state分組,降序排序
}
}
}
}
response:
{
"took": 58,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1000,
"max_score": 0,
"hits": []
},
"aggregations": {
"group_by_state": {
"doc_count_error_upper_bound": 20,
"sum_other_doc_count": 770,
"buckets": [
{
"key": "ID",
"doc_count": 27
},
...,
{
"key": "MO",
"doc_count": 20
}
]
}
}
}
--------------------------------------------------
按state分組,統(tǒng)計每個state的平均工資,并降序排序
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"size": 0,
"aggs": {
"group_by_state": {
"terms": {
"field": "state.keyword",
"order": {
"average_balance": "desc"
}
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
-------------------------------------------------
按年齡段分組,然后按性別分組,統(tǒng)計每個年齡段中不同性別的平均工資
http://192.168.0.128:9200/bank/_search
method: POST
params:
{
"size": 0,
"aggs": {
"group_by_age": {
"range": {
"field": "age",
"ranges": [
{
"from": 20,
"to": 30
},
{
"from": 30,
"to": 40
},
{
"from": 40,
"to": 50
}
]
},
"aggs": {
"group_by_gender": {
"terms": {
"field": "gender.keyword"
},
"aggs": {
"average_balance": {
"avg": {
"field": "balance"
}
}
}
}
}
}
}
}
5 打算后面一塊寫一篇文章(類似與精華翻譯了,哈哈哈),大家需要不?歡迎評論