簡介:
Curl工具是一種可以在命令行訪問url的工具,支持get和post請求方式。-X指定http請求的方法,-d指定要傳輸的數據。
創建索引:
Put創建
curl -XPUThttp://localhost:9200/shb01/student/1-d'{"name":"jack","age":30,"info":"Ilove you"}'
{"_index":"shb01","_type":"student","_id":"1","_version":1,"created":true}Youhave new mail in /var/spool/mail/root
執行put后有返回值
_index索引名稱
_type類型名
_version版本號
created:true表示是新創建的。
上面的命令每執行一次version就會加1,-XPUT必須制定id。
Post創建索引
curl -XPOSThttp://localhost:9200/shb01/student -d'{"name":"tom","age":21,"info":"tom"}'
{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":1,"created":true}
使用post創建索引數據,-XPOST可以指定id,此時如果存在相同數據則是修改,不指定id的話會隨機生成id,且每次執行都會生成新數據。
如果需要每次執行都產生新的數據可以使用post命令且不指定id。
如果使用put命令則需要增加create,命令格式如下
curl -XPUT http://localhost:9200/shb01/student/1/_create-d '{"name":"jackk","age":31}'
curl -XPUThttp://localhost:9200/shb01/student/1?op_type=create -d'{"name":"jackk","age":31}'
以上兩條命令執行時如果存在id相同的數據則會給出error信息
{"error":"DocumentAlreadyExistsException[[shb01][2][student][1]: document already exists]","status":409}
Post與put的區別
Put是等冪操作,即無論執行多少次結果都一樣,例如DEL無論刪除多少次索引庫中的結果都一樣,put只要指定了id且數據不變無論執行多少次索引庫中的數據都不變,只有version會變化。
Post每次執行都會產生新數據。
查詢
1:查詢索引庫shb01中的類型student
瀏覽器:http://192.168.79.131:9200/shb01/student/_search?pretty
Curl:curl -XGET http://192.168.79.131:9200/shb01/student/_search?pretty
其顯示結果與瀏覽器一樣。
2:查詢文檔1中的數據
http://192.168.79.131:9200/shb01/student/1?pretty
http://192.168.79.131:9200/shb01/student/1?_source&pretty
兩者結果一樣
http://192.168.79.131:9200/shb01/student/1?_source=name&pretty
可以通過source指定顯示那些字段
3:查詢所有索引庫信息
瀏覽器:http://192.168.79.131:9200/_search?pretty
將索引庫shb01和shb02的數據都顯示出來。
4:根據條件查詢
瀏覽器:http://192.168.79.131:9200/shb01/student/_search?q=name:zs&pretty
查詢name為zs的數據
5:查詢集群狀態
Curl –XGET http://192.168.79.131:9200/_cluster/health?pretty
http://192.168.79.131:9200/_cluster/health?pretty6:多索引,多類型查詢,分頁查詢,超時
Curl:curl -XGET http://192.168.79.131:9200/shb01,shb02/stu,tea/_search?pretty
curl -XGET http://192.168.79.131:9200/_all/stu,tea/_search?pretty
瀏覽器去掉curl –XGET即可
分頁
curl -XGET http://192.168.79.131:9200/shb01/stu/_search?size=2&from=0
超時
curl -XPOST http://192.168.79.131:9200/_search?_timeout=100
更新
Es
部分更新
如果文檔1的字段很多而我們只需要更新其中的一兩個字段則可以通過doc指定需要修改的字段其他字段則不必修改。
crul –XPUT
http:192.168.79.131:9200/shb01/student/1/_update?version=1
–d ‘{“doc”:{“name”:”updatename”}’
全量更新:
更新文檔1中所有字段的內容。
curl -XPUThttp://192.168.79.131:9200/shb01/student/1 -d'{"name":"will","age":100,"info":"newonw"}'
更新流程
es會將舊的文檔進行標記然后再添加新數據,舊的文檔也不能再被訪問,在后續添加數據時es會清理已經為刪除狀態的數據。
刪除
刪除文檔並不會立即生效,只會將其標記為已刪除,當后續添加更多索引時才會在后台刪除。
curl -XDELETE http://192.168.79.131:9200/shb01/student/AVad05EExskBS1Rg2tdq
根據id刪除,刪除成功返回found:true,找不到found:false,版本號都會加1。
根據條件刪除,刪除索引shb01,shb02種類型student,tea中所有name為zs的文檔
curl -XDELETEhttp://192.168.79.131:9200/shb01,shb02/student,tea/_query?q=name:zs
刪除所有的索引庫中名稱為tom的文檔
curl -XDELETE http://192.168.79.131:9200/_all/_query?q=name:tom
批處理
將一批數據加載入內存然后和es交互一次,一次性同時處理多個請求和Redis的管道類似。
格式:
Action:index/create/delete/update
Metadata:_index/_type/_id
Create:如果數據存在則報錯;index:如果數據存在仍會執行成功。
步驟:
1:在liunx下創建一個文件request1,vi request1
{"index":{"_index":"shb01","_type":"student","_id":"1"}}
{"name":"st01","age":"10","info":"st01"}
{"create":{"_index":"shb100","_type":"student","_id":"2"}}
{"name":"tea01","age":"10","info":"tea01"}
{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp"}
{"update":{"_index":"shb02","_type":"tea","_id":"1"}}
{"doc":{"name":"zszszszs"}}
文件中
index表示操作類型
_index指定索引庫,_type指定類型,_id指定操作文檔
2:執行批處理命令,關鍵字_bulk
curl -XPUThttp://192.168.79.131:9200/_bulk --data-binary @/usr/local/request1
注意:--data-binary@之間有空格隔開,我在實驗中沒有空格一直提示操作參數不對。
3:返回值
{
"took":957,"errors":false,"items":[
{"index":{"_index":"shb01","_type":"student","_id":"1","_version":12,"status":200}},
{"create":{"_index":"shb100","_type":"student","_id":"2","_version":1,"status":201}},
{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":2,"status":200,"found":true}},
{"update":{"_index":"shb02","_type":"tea","_id":"1","_version":2,"status":200}}
]
返回信息中errors表示批處理有沒有錯誤,注意version和status,其中shb100為新創建的索引庫
下面是我第二次執行request1文件的返回信息,errors為true,表示批處理中有操作執行失敗,可以看到create因為庫中已有id相同的文檔所以報錯。但是雖然存在錯誤操作但其他的操作依然成功執行。這點和redis中的事務操作類似。
{
"took":22,"errors":true,"items":[
{"index":{"_index":"shb01","_type":"student","_id":"1","_version":13,"status":200}},
{"create":{"_index":"shb100","_type":"student","_id":"2","status":409,"error":"DocumentAlreadyExistsException[[shb100][3][student][2]: document already exists]"}},
{"delete":{"_index":"shb01","_type":"student","_id":"AVadzuNgxskBS1Rg2tdp","_version":1,"status":404,"found":false}},
{"update":{"_index":"shb02","_type":"tea","_id":"1","_version":3,"status":200}}
]
}
4:在命令中指定索引庫和類型
創建一個文件,文件中沒有配置索引庫和類型
{"index":{"_id":"1"}}
{"name":"st1_1","age":"10","info":"st1_1"}
{"create":{"_id":"200"}}
{"name":"st200","age":"10","info":"st200"}
執行如下命令,在命令中指定了索引庫和類型
curl -XPUThttp://192.168.79.131:9200/shb01/student/_bulk --data-binary@/usr/local/request2
返回信息
{
"took":24,"errors":false,"items":[
{"index":{"_index":"shb01","_type":"student","_id":"1","_version":17,"status":200}},
{"create":{"_index":"shb01","_type":"student","_id":"200","_version":1,"status":201}}
]
}
5:也可以使用-XPOST替換-XPUT