【大数据----ElasticSearch】命令2

【命令2】

elasticsearch的rest访问格式:

curl -X<REST Verb> <Node>:<Port>/<Index>/<Type>/<ID>

 

1、启动

[es@vm1 bin]$ ./elasticsearch --cluster.name myes --node.name node1

2、查看集群状态

[es@vm1 ~]$ curl http://vm1:9200/_cat/health?v
epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks 
1432704777 22:32:57 myes green

3、查看节点列表

[es@vm1 ~]$ curl http://vm1:9200/_cat/nodes?v
host ip heap.percent ram.percent load node.role master name 
vm1 192.168.1.111 3 24 0.04 d * node1

4、查看index列表

[es@vm1 ~]$ curl http://vm1:9200/_cat/indices?v
health status index pri rep docs.count docs.deleted store.size pri.store.size

5、创建index

[es@vm1 ~]$ curl -XPUT http://vm1:9200/customer?pretty 
{
  "acknowledged" : true
}

6、添加一个document

[es@vm1 ~]$ curl -XPUT vm1:9200/customer/external/1?pretty -d '{"name":"lisg"}'
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_version" : 4,
  "created" : true
}

7、检索一个document

[es@vm1 ~]$ curl -XGET vm1:9200/customer/external/1?pretty
{
    "_index" : "customer",
    "_type" : "external",
    "_id" : "1",
    "_version" : 4,
    "found" : true,
    "_source":{"name":"lisg"}
}

8、删除一个document

[es@vm1 ~]$ curl -XDELETE vm1:9200/customer/external/1?pretty
{
    "found" : true,
    "_index" : "customer",
    "_type" : "external",
    "_id" : "1",
    "_version" : 5
}

9、删除一个type

[es@vm1 ~]$ curl -XDELETE vm1:9200/customer/external?pretty
{
    "acknowledged" : true
}

10、删除一个index

[es@vm1 ~]$ curl -XDELETE vm1:9200/customer?pretty
{
    "acknowledged" : true
}

11、POST方式可以添加一个document,不用指定ID

[es@vm1 ~]$ curl -XPOST vm1:9200/customer/external?pretty -d '{"name":"zhangsan"}'
{
    "_index" : "customer",
    "_type" : "external",
    "_id" : "AU2UAazzBzlrcKeIwh7T",
    "_version" : 1,
    "created" : true
}

12、使用doc更新document

[es@vm1 ~]$ curl -XPUT vm1:9200/customer/external/1?pretty -d '{"name":"lisg4", "age":28}'
{
    "_index" : "customer",
    "_type" : "external",
    "_id" : "1",
    "_version" : 4,
    "created" : false
}

13、使用script更新document(1.4.3版本动态脚本是被禁止的)

[es@vm1 ~]$ curl -XPOST vm1:9200/customer/external/1/_update?pretty -d '{"script":"ctx._source.age += 5"}'
{
  "error" : "ElasticsearchIllegalArgumentException[failed to execute script]; nested: ScriptException[dynamic scripting for [groovy] disabled]; ",
  "status" : 400
}

14、查询全部

[es@vm1 ~]$ curl -XGET vm1:9200/customer/external/_search?pretty
{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
  "total" : 5,
  "successful" : 5,
  "failed" : 0
},
"hits" : 
{
  "total" : 3,
  "max_score" : 1.0,
  "hits" : [ {
  "_index" : "customer",
  "_type" : "external",
  "_id" : "AU2UAazzBzlrcKeIwh7T",
  "_score" : 1.0,
  "_source":{"name":"zhangsan"}
}, 
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "1",
  "_score" : 1.0,
  "_source":{"name":"lisg4", "age":28}
}, 
{
  "_index" : "customer",
  "_type" : "external",
  "_id" : "2",
  "_score" : 1.0,
  "_source":{"name":"dengsl"}
} ]
}
}

15、批量操作,{}之间要换行

[es@vm1 ~]$ curl -XPOST vm1:9200/customer/external/_bulk?pretty -d '
> {index:{_id:3}}
> {name:"zhangsan", age:28}
> {index:{_id:4}}
> {name:"lisi", age:29}
> {update:{_id:4}}
> {doc:{name:"lisi2", age:30}}
> {delete:{_id:"AU2UAazzBzlrcKeIwh7T"}}
> '
{
  "took" : 34,
  "errors" : false,
  "items" : [ 
  {
    "index" : {
    "_index" : "customer",
    "_type" : "external",
    "_id" : "3",
    "_version" : 1,
    "status" : 201
  }
}, 
{
  "index" : {
    "_index" : "customer",
    "_type" : "external",
    "_id" : "4",
    "_version" : 1,
    "status" : 201
  }
}, 
{
  "update" : {
    "_index" : "customer",
    "_type" : "external",
    "_id" : "4",
    "_version" : 2,
    "status" : 200
  }
}, 
{
  "delete" : {
    "_index" : "customer",
    "_type" : "external",
    "_id" : "AU2UAazzBzlrcKeIwh7T",
    "_version" : 2,
    "status" : 200,
    "found" : true
  }
} ]
}

上面的语句顺序执行的操作有:
1)添加索引:张三,28
2)添加索引:李四,29
3)更新李四:李四2,30
4)删除索引:id是AU2UAazzBzlrcKeIwh7T的索引

16、从文件中加载数据(accounts.json见附件)

[es@vm1 ~]$ curl -XPOST http://vm1:9200/customer/external/_bulk?pretty --data-binary @accounts.json 
[es@vm1 ~]$ curl -XGET vm1:9200/_cat/indices?v
health status index pri rep docs.count docs.deleted store.size pri.store.size 
yellow open customer 5 1 1000 0 442.3kb 442.3kb

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值