常用命令請(qǐng)求
- 獲取健康狀況
GET /_cat/health?v
- 獲取集群中節(jié)點(diǎn)列表
GET /_cat/nodes?v
- 獲取索引列表
GET /_cat/indices?v
- 創(chuàng)建名字為customer的索引
PUT /customer?pretty
- 給customer索引存入數(shù)據(jù)
PUT /customer/doc/1?pretty
{
"name": "John Doe"
}
Elasticsearch并不要求你在索引文檔之前先顯性的創(chuàng)建索引。在4硅堆、5步驟中宛瞄,如果在執(zhí)行步驟5時(shí),customer索引還沒(méi)有創(chuàng)建,Elasticsearch將會(huì)自動(dòng)的創(chuàng)建這個(gè)索引
- 刪除索引
DELETE /customer?pretty
訪問(wèn)數(shù)據(jù)命令模板為:<REST Verb> /<Index>/<Type>/<ID>
- 修改數(shù)據(jù)
PUT /customer/doc/1?pretty
{
"name": "Jane Doe"
}
- 非顯性的指定id科盛,elasticsearch會(huì)自動(dòng)創(chuàng)建
POST /customer/doc?pretty
{
"name": "Jane Doe"
}
- 更新文檔
POST /customer/doc/1/_update?pretty
{
"doc": { "name": "Jane Doe" }
}
- 更新并添加數(shù)據(jù)
POST /customer/doc/1/_update?pretty
{
"doc": { "name": "Jane Doe", "age": 20 }
}
- 刪除文檔
DELETE /customer/doc/2?pretty
- 批量操作妇多,使用_bulk
- 索引兩個(gè)文檔
POST /customer/doc/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
- 更新id=1的文檔,刪除id=2的文檔
POST /customer/doc/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
- 搜索洼专,通過(guò)_search
- 通過(guò)請(qǐng)求URL
GET /bank/_search?q=*&sort=account_number:asc&pretty //返回bank索引的所有文檔
- q=* 匹配bank索引中的所有文檔
- sort=account_number:asc 使用每個(gè)文檔中的account_number字段進(jìn)行升序排序
- pretty 告訴Elasticsearch返回 pretty-printed JSON 結(jié)果
- 通過(guò)請(qǐng)求體
GET /bank/_search
{
"query": { "match_all": {} },
"sort": [
{ "account_number": "asc" }
]
}
- 結(jié)果解析
{
"took" : 63,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 1000,
"max_score" : null,
"hits" : [ {
"_index" : "bank",
"_type" : "account",
"_id" : "0",
"sort": [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"}
}, {
"_index" : "bank",
"_type" : "account",
"_id" : "1",
"sort": [1],
"_score" : null,
"_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"}
}, ...
]
}
}
- took Elasticsearch查詢耗費(fèi)時(shí)間棒掠,單位毫秒
- timed_out 查詢是否超時(shí)
- _shards 查詢了多少個(gè)分片,和查詢成功/跳過(guò)/失敗的分片的數(shù)量
- hits 查詢結(jié)果
- hits.total 匹配搜索條件的所有文檔數(shù)
- hits.hits 搜索結(jié)果的實(shí)際數(shù)組(默認(rèn)前10個(gè)文檔)
- hits.sort 結(jié)果排序的key
- hits._score 和 max_score 現(xiàn)在可以忽略的字段