Elasticsearch 基本命令

建立一個索引

Elasticsearch 命令的通常格式是:REST VERBHOST:9200/index/doc-type— 其中 REST VERB 是 PUT、GET 或 DELETE。(使用 cURL -X 動詞前綴來明確指定 HTTP 方法。)shell

要建立一個索引,可在您的 shell 中運行如下命令:json

curl -XPUT 「http://localhost:9200/music/app

插入一個文檔

要在 /music 索引下建立一個類型,可插入一個文檔。在第一個示例中,您的文檔包含數據(包含一行)「Deck the Halls」 的歌詞,這是一首最初由威爾士詩人 John Ceirog Hughes 於 1885 年編寫的傳統的聖誕歌曲。curl

要將包含 「Deck the Halls」 的文檔插入索引中,可運行如下命令(將該命令和本教程的其餘 cURL 命令都鍵入到一行中):url

curl -XPUT "http://localhost:9200/music/songs/1" -d 
 '{ "name": "Deck the Halls", "year": 1885, "lyrics": "Fa la la la la" }'

運行以上命令可能出現異常錯誤: 
{「error」:」Content-Type header [application/x-www-form-urlencoded] is not supported」,」status」:406}spa

這是由於沒有指定內容的格式致使 所以須要在命令裏面指定header 命令改成:code

curl -H "Content-Type: application/json" -XPUT 
 "http://localhost:9200/music/songs/1" -d 
 '{"name":"Deck the Halls","year":1885,"lyrics":"Fa la la la la"}'

再次運行 成功 並返回成功信息orm

{"_index":"music","_type":"songs","_id":"2",
 "_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}

<—– 注意—–>blog

這裏的music爲索引 而songs爲類型 一個索引下面只能爲一個類型教程

下面的命令能夠列出每一個 Index 所包含的 Type

curl 'localhost:9200/_mapping?pretty=true'

查看文檔

要查看該文檔,可以使用簡單的 GET 命令:

curl -XGET "http://localhost:9200/music/songs/1"

Elasticsearch 使用您以前 PUT 進索引中的 JSON 內容做爲響應:

{"_index":"music","_type":"songs","_id":"1","_version":1,
 "found":true,"_source":{ "name": "Deck the Halls", "year": 1885, "lyrics": "Fa la la la la" }}

更新文檔

es的更新命令和插入命令一致

curl -H "Content-Type: application/json" 
 -XPUT "http://localhost:9200/music/songs/1" -d 
 '{"name":"Deck the Halls","year":  1886,"lyrics":"Fa la la la la" }'

這裏指出了須要更新的文檔的id所以會返回 更新成功。

{"_index":"music","_type":"songs","_id":"2","_version":2,
"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":1,"_primary_term":1}

刪除文檔##

刪除文檔使用 -XDELETE

curl -XDELETE "http://localhost:9200/music/songs/1"

返回信息:

{"_index":"music","_type":"songs","_id":"1","_version":3,
"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":2,"_primary_term":1}

查看索引

curl -X GET 'http://localhost:9200/_cat/indices?v'

這裏寫圖片描述

經常使用命令: 
http://192.168.30.70:9200/_cat/ //顯示命令提示 
這裏寫圖片描述

curl -X POST "http://localhost:9200/music/_open"   打開索引   關閉索引 (_close)

    curl -X GET 'http://localhost:9200/_cat/indices?v'  查看索引

    curl 'localhost:9200/_mapping?pretty=true'    列出每一個 Index 所包含的 Type

    curl 'localhost:9200/accounts/person/_search'   查看某個索引下所有記錄
相關文章
相關標籤/搜索