文檔的基本操作
文檔的基本操作
新增文檔
# 創(chuàng)建索引 curl -X PUT "http://localhost:9200/student" # 創(chuàng)建mapping curl -X PUT "localhost:9200/student/_mapping" -H 'Content-Type: application/json' -d' { "properties": { "name": { "type": "text" }, "years":{ "type": "integer" } } } ' # 指定ID新增文檔 curl -X PUT "localhost:9200/student/_doc/1" -H "Content-Type: application/json" -d' { "name": "Nick", "age": 19 } ' # 不指定ID新增文檔 curl -X POST "localhost:9200/student/_doc" -H "Content-Type: application/json" -d' { "name": "Nick", "age": 19 } '
指定操作類型
# 創(chuàng)建文檔,如果該文檔已經(jīng)存在則會(huì)UPDATE curl -X PUT "localhost:9200/student/_doc/1" -H "Content-Type: application/json" -d' { "name": "Nick", "age": 35 } ' # 指定創(chuàng)建操作,如果該文檔已經(jīng)存在則會(huì)報(bào)錯(cuò),該操作可以避免錯(cuò)誤 curl -X PUT "localhost:9200/student/_doc/1?op_type=create" -H "Content-Type: application/json" -d' { "name": "Nick", "age": 35 } '
查看文檔
# 通過(guò)ID查看文檔 curl -X GET "localhost:9200/student/_doc/1" curl -X POST "localhost:9200/_mget?pretty" -H "Content-Type: application/json" -d ' { "docs":[ { "_index": "student", "_type": "_doc", "_id": "1" }, { "_index": "school", "_type": "_doc", "_id": "1" } ] } ' # 指定索引,然后獲取多個(gè)ID值的文檔 curl -X POST "localhost:9200/student/_mget?pretty" -H "Content-Type: application/json" -d ' { "docs":[ { "_type": "_doc", "_id": "1" }, { "_type": "_doc", "_id": "2" } ] } ' # 指定索引,文檔,然后獲取多個(gè)ID值的文檔 curl -X POST "localhost:9200/student/_doc/_mget?pretty" -H "Content-Type: application/json" -d ' { "docs":[ { "_id": "1" }, { "_id": "2" } ] } ' curl -X POST "localhost:9200/student/_doc/_mget?pretty" -H "Content-Type: application/json" -d ' { "ids": [1,2] } '
修改文檔
# 指定id修改 curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d ' { "doc": { "name": "Elaine" } } ' curl -X GET "localhost:9200/student/_doc/1?pretty" # 新增字段,ctx上下文 curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d ' { "script": "ctx._source.age1 = 19" } ' # 刪除字段,ctx上下文 curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d ' { "script": "ctx._source.remove(\"age1\")" } ' # 更新, upsert當(dāng)文檔不存在時(shí),upsert內(nèi)的內(nèi)容將會(huì)插入到索引中,作為一個(gè)新文檔 curl -X POST "localhost:9200/student/_update/1" -H "Content-Type: application/json" -d ' { "script": { "source": "ctx._source.age += params.age", "params": { "age": 4 } }, "upsert":{ "age": 1 } } '
刪除
# 刪除指定文檔 curl -X DELETE "localhost:9200/student/_doc/1" -H "Content-Type: application/json"
自動(dòng)創(chuàng)建索引
當(dāng)索引不存在,并且auto_create_index為true的時(shí),新增文檔會(huì)自動(dòng)創(chuàng)建索引
# 查看方法 curl http://localhost:9200/_cluster/settings # 配置方法 curl -X PUT "localhost:9200/_cluster/settings" -H "Content-Type: application/json" -d' { "persistent": { "action.auto_create_index": "true" } } '
Elasticsearch
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。