ElasticSearch基本操作——curl
ElasticSearch基本操作——curl
获取集群信息
[yangqi@xiaoer ~]$ curl -XGET 'xiaoer:9200'

查看es集群是否健康
[yangqi@xiaoer ~]$ curl -XGET 'xiaoer:9200/_cat/health?v'

查看节点列表
[yangqi@xiaoer ~]$ curl -XGET 'xiaoer:9200/_cat/nodes?v'

查看es集群所有的索引
[yangqi@xiaoer ~]$ curl -XGET 'xiaoer:9200/_cat/indices?v'

创建索引
新建一个名叫blog的index
[yangqi@xiaoer ~]$ curl -XPUT 'xiaoer:9200/blog'
# 加上 pretty 可以使用 JSON 格式输出
[yangqi@xiaoer ~]$ curl -XPUT 'xiaoer:9200/blog?pretty'
# 创建索引时指定分区数量/副本数
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/blog' -d '{
"index": {
"number_of_shards": 2,
"number_of_replicas": 1
}
}'
查询索引
# 查询单个索引
[yangqi@xiaoer ~]$ curl -XGET 'http://xiaoer:9200/blog?pretty'
[yangqi@xiaoer ~]$ curl -XGET 'http://xiaoer:9200/blog/_settings?pretty'
# 查询多个索引
[yangqi@xiaoer ~]$ curl -XGET 'http://xiaoer:9200/blog,blog1/_settings?pretty'
# 查询所以索引
[yangqi@xiaoer ~]$ curl -XGET 'http://xiaoer:9200/_all/_settings?pretty'
? 更改索引副本
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/_template/templatehttprequestrecord?pretty' -d '{
"indexpatterns": ["*"],
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
}
}'
删除索引
# 删除指定 id 的文档
[yangqi@xiaoer ~]$ curl -XDELETE 'http://xiaoer:9200/library/books/2?pretty'
# 删除
[yangqi@xiaoer ~]$ curl -XDELETE 'http://xiaoer:9200/library/books?pretty'
# 删除索引
[yangqi@xiaoer ~]$ curl -XDELETE 'http://xiaoer:9200/library?pretty'
添加文档
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/1' -d '{
"title": "this is es book",
"name": {
"first": "lao",
"last": "six"
},
"publish_date": "2000-02-02",
"price": 199.9
}'
# 不指定 id,一定要用 POST 提交
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPOST 'http://xiaoer:9200/library/books?pretty' -d '{
"title": "this is es book",
"name": {
"first": "lao",
"last": "wang"
},
"publish_date": "2001-02-03",
"price": 299.9
}'
# 指定 op_type 创建:(op_type=create,表示不存在就创建,存在就报错;等价于 _create)
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/1?op_type=create' -d '{
"title": "this is es book",
"name": {
"first": "lao",
"last": "sun"
},
"publish_date": "2000-03-04",
"price": 200.9
}'
# 指定 _create 创建
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/3/_create' -d '{
"title": "this is es book",
"name": {
"first": "lao",
"last": "sun"
},
"publish_date": "2000-04-05",
"price":300
}'
# 不指定 op_type,_create:(覆盖操作)
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/3?pretty' -d '{
"title": "this is java book",
"name": {
"first": "li",
"last": "jun"
},
"publish_date": "1998-09-08",
"price": 34.5
}'
# 指定 routing
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/2?routing=this' -d '{
"title": "this is routing",
"name": {
"first": "sun",
"last": "qiang"
},
"publish_date": "1889-08-08",
"price": 78.3
}'
# 指定 timeout
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/4?timeout=5m' -d '{
"title": "this is timeout",
"name": {
"first": "sun",
"last": "wang"
},
"publish_date": "1999-08-07",
"price": 2000
}'

获取文档
# 指定 id
[yangqi@xiaoer ~]$ curl -XGET 'xiaoer:9200/library/books/1?pretty'
# 指定获取字段
[yangqi@xiaoer ~]$ curl -XGET 'http://xiaoer:9200/library/books/1?_source=price&pretty'
# 查询多个 id:多个索引查询都可以
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XGET 'http://xiaoer:9200/_mget?pretty' -d '{
"docs": [
{
"_index": "library",
"_type": "books",
"_id": 1
},
{
"_index": "library",
"_type": "books",
"_id": 2
}
]
}'
# 查询多个 id 和字段
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XGET 'http://xiaoer:9200/_mget?pretty' -d '{
"docs": [
{
"_index": "library",
"_type": "books",
"_id": 1,
"_source": [
"title", "price"
]
},
{
"_index": "library",
"_type": "books",
"_id", 3
}
]
}'
# 查询多个 id
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XGET 'http://xiaoer:9200/library/books/_mget?pretty' -d '{
"ids": ["1", "2"]
}'

更新文档
# 指定 id,覆盖更新,如果没有指定的字段,则为空
[yangqi@xiaoer ~]$ curl -H 'Content-Type: application/json' -XPUT 'http://xiaoer:9200/library/books/1?pretty' -d '{
"price": 3000
}'
# 局部更新:需要 POST,_update,doc
[yangqi@xiaoer ~]$ curl -H "Content-Type: application/json" -XPOST 'xiaoer:9200/library/books/1/_update?pretty' -d '{
"doc": {
"name": {
"first": "lao",
"last": "liu"
}
}
}'

查看更新的文档
[yangqi@xiaoer ~]$ curl -XGET 'xiaoer:9200/library/books/1?pretty'


浙公网安备 33010602011771号