Elasticsearch Restful Web API 的使用


新建和刪除Index
新建 Index,可以直接向 Elastic 服務器發(fā)出 PUT 請求。下面的例子是新建一個名叫weather的 Index。
$ curl -X PUT 'localhost:9200/weather'
服務器返回一個 JSON 對象,里面的acknowledged字段表示添加操作成功。
{
"acknowledged":true,
"shards_acknowledged":true
}
然后,我們發(fā)出 DELETE 請求,刪除這個 Index。
$ curl -X DELETE 'localhost:9200/weather'
服務器返回一個 JSON 對象,里面的acknowledged字段表示刪除操作成功。
{
"acknowledged":true
}
數(shù)據(jù)操作
新增記錄
向指定的 /Index/Type 發(fā)送 PUT 請求,就可以在 Index 里面新增一條記錄。比如,向/accounts/person發(fā)送請求,就可以新增一條人員記錄。
$ curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user": "張三",
"title": "工程師",
"desc": "數(shù)據(jù)庫管理"
}'
服務器返回的 JSON 對象,會給出 Index、Type、Id、Version 等信息。
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_version":1,
"result":"created",
"_shards":{"total":2,"successful":1,"failed":0},
"created":true
}
如果你仔細看,會發(fā)現(xiàn)請求路徑是/accounts/person/1,最后的1是該條記錄的 Id。它不一定是數(shù)字,任意字符串(比如abc)都可以。
新增記錄的時候,也可以不指定 Id,這時要改成 POST 請求。
$ curl -X POST 'localhost:9200/accounts/person' -d '
{
"user": "李四",
"title": "工程師",
"desc": "系統(tǒng)管理"
}'
上面代碼中,向/accounts/person發(fā)出一個 POST 請求,添加一個記錄。這時,服務器返回的 JSON 對象里面,_id字段就是一個隨機字符串。
{
"_index":"accounts",
"_type":"person",
"_id":"AV3qGfrC6jMbsbXb6k1p",
"_version":1,
"result":"created",
"_shards":{"total":2,"successful":1,"failed":0},
"created":true
}
注意,如果沒有先創(chuàng)建 Index(這個例子是accounts),直接執(zhí)行上面的命令,Elastic 也不會報錯,而是直接生成指定的 Index。所以,打字的時候要小心,不要寫錯 Index 的名稱。
查看記錄
向/Index/Type/Id發(fā)出 GET請求,就可以查看這條記錄。
$ curl 'localhost:9200/accounts/person/1?pretty=true'
上面代碼請求查看/accounts/person/1這條記錄,URL 的參數(shù)pretty=true表示以易讀的格式返回。
返回的數(shù)據(jù)中,found字段表示查詢成功,_source字段返回原始記錄。
{
"_index" : "accounts",
"_type" : "person",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source" : {
"user" : "張三",
"title" : "工程師",
"desc" : "數(shù)據(jù)庫管理"
}
}
如果 Id 不正確,就查不到數(shù)據(jù),found字段就是false。
$ curl 'localhost:9200/weather/beijing/abc?pretty=true'
{
"_index" : "accounts",
"_type" : "person",
"_id" : "abc",
"found" : false
}
刪除記錄
刪除記錄就是發(fā)出 DELETE 請求。
$ curl -X DELETE 'localhost:9200/accounts/person/1'
更新記錄
更新記錄就是使用 PUT 請求,重新發(fā)送一次數(shù)據(jù)。
$ curl -X PUT 'localhost:9200/accounts/person/1' -d '
{
"user" : "張三",
"title" : "工程師",
"desc" : "數(shù)據(jù)庫管理,軟件開發(fā)"
}'
執(zhí)行成功后返回:
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_version":2,
"result":"updated",
"_shards":{"total":2,"successful":1,"failed":0},
"created":false
}
上面代碼中,我們將原始數(shù)據(jù)從"數(shù)據(jù)庫管理"改成"數(shù)據(jù)庫管理,軟件開發(fā)"。 返回結果里面,有幾個字段發(fā)生了變化。
"_version" : 2,
"result" : "updated",
"created" : false
可以看到,記錄的 Id 沒變,但是版本(version)從1變成2,操作類型(result)從created變成updated,created字段變成false,因為這次不是新建記錄。
數(shù)據(jù)查詢
返回所有記錄
使用 GET 方法,直接請求/Index/Type/_search,就會返回所有記錄。
$ curl 'localhost:9200/accounts/person/_search'
{
"took":2,
"timed_out":false,
"_shards":{"total":5,"successful":5,"failed":0},
"hits":{
"total":2,
"max_score":1.0,
"hits":[
{
"_index":"accounts",
"_type":"person",
"_id":"AV3qGfrC6jMbsbXb6k1p",
"_score":1.0,
"_source": {
"user": "李四",
"title": "工程師",
"desc": "系統(tǒng)管理"
}
},
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_score":1.0,
"_source": {
"user" : "張三",
"title" : "工程師",
"desc" : "數(shù)據(jù)庫管理,軟件開發(fā)"
}
}
]
}
}
上面代碼中,返回結果的 took字段表示該操作的耗時(單位為毫秒),timed_out字段表示是否超時,hits字段表示命中的記錄,里面子字段的含義如下。
- total:返回記錄數(shù),本例是2條。
- max_score:最高的匹配程度,本例是1.0。
- hits:返回的記錄組成的數(shù)組。
返回的記錄中,每條記錄都有一個_score字段,表示匹配的程序,默認是按照這個字段降序排列。
全文搜索
Elastic 的查詢非常特別,使用自己的查詢語法,要求 GET 請求帶有數(shù)據(jù)體。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "軟件" }}
}'
上面代碼使用 Match 查詢,指定的匹配條件是desc字段里面包含"軟件"這個詞。返回結果如下。
{
"took":3,
"timed_out":false,
"_shards":{"total":5,"successful":5,"failed":0},
"hits":{
"total":1,
"max_score":0.28582606,
"hits":[
{
"_index":"accounts",
"_type":"person",
"_id":"1",
"_score":0.28582606,
"_source": {
"user" : "張三",
"title" : "工程師",
"desc" : "數(shù)據(jù)庫管理,軟件開發(fā)"
}
}
]
}
}
Elastic 默認一次返回10條結果,可以通過size字段改變這個設置。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"size": 1
}'
上面代碼指定,每次只返回一條結果。
還可以通過from字段,指定位移。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "管理" }},
"from": 1,
"size": 1
}'
上面代碼指定,從位置1開始(默認是從位置0開始),只返回一條結果。
邏輯運算
如果有多個搜索關鍵字, Elastic 默認它們是or關系。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query" : { "match" : { "desc" : "軟件 系統(tǒng)" }}
}'
上面代碼搜索的是軟件 or 系統(tǒng)。
如果要執(zhí)行多個關鍵詞的and搜索,必須使用布爾查詢。
$ curl 'localhost:9200/accounts/person/_search' -d '
{
"query": {
"bool": {
"must": [
{ "match": { "desc": "軟件" } },
{ "match": { "desc": "系統(tǒng)" } }
]
}
}
}'