Elasticsearch 基本命令
时间: 2018-09-17来源:OSCHINA
前景提要
「深度学习福利」大神带你进阶工程师,立即查看>>>
创建一个索引
Elasticsearch 命令的一般格式是:REST VERBHOST:9200/index/doc-type— 其中 REST VERB 是 PUT、GET 或 DELETE。(使用 cURL -X 动词前缀来明确指定 HTTP 方法。)
要创建一个索引,可在您的 shell 中运行以下命令:
curl -XPUT “ http://localhost:9200/music/ ”
插入一个文档
要在 /music 索引下创建一个类型,可插入一个文档。在第一个示例中,您的文档包含数据(包含一行)“Deck the Halls” 的歌词,这是一首最初由威尔士诗人 John Ceirog Hughes 于 1885 年编写的传统的圣诞歌曲。
要将包含 “Deck the Halls” 的文档插入索引中,可运行以下命令(将该命令和本教程的其他 cURL 命令都键入到一行中): 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}
这是因为没有指定内容的格式导致 因此需要在命令里面指定header 命令改为: 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"}'
再次运行 成功 并返回成功信息 {"_index":"music","_type":"songs","_id":"2", "_version":1,"result":"created","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":0,"_primary_term":1}
<—– 注意—–>
这里的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' 查看某个索引下全部记录

科技资讯:

科技学院:

科技百科:

科技书籍:

网站大全:

软件大全:

热门排行